Stack Overflow for Teams is a private, secure spot for you and
your coworkers to find and share information.
Learn more
class Person(models.Model):
name = models.CharField()
address_streetname = models.CharField()
address_housenumber = models.CharField()
address_zip = models.CharField()
I have a django rest framework ModelSerializer
that exposes all the fields.
But I would like to be able to serialize the address fields into a dict. So when serialized to json output would be:
name: 'Some name',
address: {
streetname: 'This is a test',
housenumber: '23',
zip: '1337',
I tried creating creating a AddressSerializer
class Address(object):
class AddressSerializer(serializers.Serializer):
streetname = serializers.CharField()
housenumber = serializers.CharField()
zip = serializers.CharField()
and then set the PersonSerializer.address
to use the AddressSerializer
class PersonSerializer(serializers.ModelSerializer):
address = AddressSerializer()
This results in my schema being correct. I generate swagger docs by using drf-yasg
. It looks at the serializers to generate the correct model definitions. So the serializers needs to represent the schema.
So this is where I am at, at the moment. Obviously now it fails because there is no address
property in the Person
model. How would you go about solving this?
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, or for fields which
require access to the complete object in order to determine the output
representation.
So,try this,
class AddressSerializer(serializers.Serializer):
streetname = serializers.CharField(source='address_streetname')
housenumber = serializers.CharField(source='address_housenumber')
zip = serializers.CharField(source='address_zip')
class PersonSerializer(serializers.ModelSerializer):
# .... your fields
address = AddressSerializer(source='*')
class Meta:
fields = ('address', 'other_fields')
model = Person
class Person(models.Model):
name = models.CharField()
address_streetname = models.CharField()
address_housenumber = models.CharField()
address_zip = models.CharField()
@property
def address(self):
return {'streetname': self.address_streetname,
'housenumber': self.address_housenumber,
'zip': self.address_zip}
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.