Django Rest Framework: Create And Update An Object With A Nested Object Value (instead Of Primary Key)
I have two models one Country and one Office model. The office model has a ForeignKey to the Country model: class Country(TranslatableModel): iso = models.CharField( ma
Solution 1:
Read-only
To simply send that representation to the client (read-only, not dealing with creating objects from their deserialized representation)
classOfficeSerializer(serializers.ModelSerializer):
country = serializers.Field(source='country.iso') # this field of the serializer
# is read-only
As you can see, it'll read will read country.iso
from your office
instance, which resolves to 'us'
e.g., then gets put into a serializer key called 'country'
, given you an output of {'country': 'us'}
Writable nested field
Now to complete this, let's write a custom OfficeSerializer.create()
:
defcreate(self, validated_data):
# This expects an input format of {"country": "iso"}# Instead of using the ID/PK of country, we look it up using iso field
country_iso = validated_data.pop('country')
country = Country.objects.get(iso=country_iso)
# Create the new Office object, and attach the country object to it
office = Office.objects.create(country=country, **validated_data)
# Notice I've left **validated_data in the Office object builder,# just in case you want to send in more fields to the Office model# Finally a serializer.create() is expected to return the object instancereturn office
As for a OfficeSerializer.update()
it's similar:
defupdate(self, instance, validated_data):
# instance is your Office object# You should update the Office fields here# instance.field_x = validated_data['field_x']# Let's grab the Country object again
country_iso = validated_data.pop('country')
country = Country.objects.get(iso=country_iso)
# Update the Office object
instance.country = country
instance.save()
return instance
Post a Comment for "Django Rest Framework: Create And Update An Object With A Nested Object Value (instead Of Primary Key)"