[Answered ]-Django serializer with nested column

1👍

✅

Haven’t tested this but it looks like this question. Try to use a SerializerMethodField.

1👍

I think you should be fine with a regular Field (note that this is readonly).

class ResourceSerializer(serializers.ModelSerializer):
    owner_name = serializers.Field(source='resmst_owner.owner_name')

    class Meta:
        model = Resmst
        resource_name = 'resmst'
        fields = ('resmst_id', 'resmst_name', 'resmst_desc', 'resmst_limit', 
            'resmst_inuse', 'resmst_active', 'resmst_lstchgtm', 
            'resmst_prntid', 'resmst_owner', 'resmst_public', 
            'resmst_locked', 'resmst_offline', 'owner_name', )
        read_only_fields = ('resmst_id',)

Don’t forget to add it to Meta.fields (as above).

See the section on Core arguments:

source

The name of the attribute that will be used to populate the
field. May be a method that only takes a self argument, such as
Field(source=’get_absolute_url’), or may use dotted notation to
traverse attributes, such as Field(source=’user.email’).

The value source=’*’ has a special meaning, and is used to indicate
that the entire object should be passed through to the field. This can
be useful for creating nested representations. (See the implementation
of the PaginationSerializer class for an example.)

Defaults to the name of the field.

Leave a comment