Django Rest Framework Model Id Field In Nested Relationship Serializer
I'm usign Django Rest Framework where I have the following two serializers: class ServiceSerializer(serializers.ModelSerializer): id = serializers.ReadOnlyField() class M
Solution 1:
Ok - I think I found the answer after ... carefully reading the docs :)
So according to the docs the id
field could be set to a ModelField like this:
id = serializers.ModelField(model_field=ServiceType()._meta.get_field('id'))
Indeed, after adding this line the id
field is present in validated_data :)
Solution 2:
The accepted answer works but is unnecessarily complicated. You can just change the ID field to not be readonly. E.g. something like this will do:
id = serializers.IntegerField(required=False)
Solution 3:
You have set id
as a ReadOnlyField
. That's why id
does not appear in validated data. Just remove this line:
id = serializers.ReadOnlyField()
Solution 4:
The more straightforward option:
id = serializers.IntegerField(write_only=False)
Post a Comment for "Django Rest Framework Model Id Field In Nested Relationship Serializer"