While using Django-REST-Framework, I am not able to display selected fields in a nested serialized object.
I am correctly able to serialize the entirety of the Ad
Additionally to the other answers, I would like to add the following in case somebody lands here too.
The nested serialization requires the field name to be in the model fields in order to work.
In case that we need a reverse relation, then obviously there will be no such field in the model fields.
For example, with the particular models in the question, this would happen if we needed to reference all outlets in the address serializer.
Therefore we need to: (a) Either specify a related_name
to the foreign key of the other model. This is where we would use outlet_address
, as this is the related name of address field in outlet model.
Or (b) if no related name then use the default Django name, appending _set
to the model name (eg outlet_set
.