1

I'm calling the following serializer -

class ResourceSerializer(serializers.ModelSerializer):

    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')
        read_only_fields = ('resmst_id',)

resmst_owner is a FK relation to another table. What I want to do is have the serializer display the information from a column where that FK relates.

This is the current json -

[
    {
        "resmst_id": 204, 
        "resmst_name": "GAWK", 
        "resmst_desc": null, 
        "resmst_limit": 1, 
        "resmst_inuse": 0, 
        "resmst_active": "Y", 
        "resmst_lstchgtm": "2014-08-20T11:15:18", 
        "resmst_prntid": null, 
        "resmst_owner": 822, 
        "resmst_public": "Y", 
        "resmst_locked": null, 
        "resmst_offline": 0
    }
]

And this is how I want it to look -

[
    {
        "resmst_id": 204, 
        "resmst_name": "GAWK", 
        "resmst_desc": null, 
        "resmst_limit": 1, 
        "resmst_inuse": 0, 
        "resmst_active": "Y", 
        "resmst_lstchgtm": "2014-08-20T11:15:18", 
        "resmst_prntid": null, 
        "owner_name": "John Smith", 
        "resmst_public": "Y", 
        "resmst_locked": null, 
        "resmst_offline": 0
    }
]

Or am I stuck with having to do it this way -

[
    {
        "resmst_id": 204, 
        "resmst_name": "GAWK", 
        "resmst_desc": null, 
        "resmst_limit": 1, 
        "resmst_inuse": 0, 
        "resmst_active": "Y", 
        "resmst_lstchgtm": "2014-08-20T11:15:18", 
        "resmst_prntid": null, 
        "resmst_owner": {
            "owner_name": "John Smith"
        }, 
        "resmst_public": "Y", 
        "resmst_locked": null, 
        "resmst_offline": 0
    }
]

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you pointed me in the right direction with the help of @Steven I got it working.
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.

1 Comment

Thanks the first one got me the solution but this one gave me the more elegant "what I was looking for".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.