32👍
✅
Any AutoField
s on your model (which is what the automatically generated id
key is) are set to read-only by default when Django REST Framework is creating fields in the background. You can confirm this by doing
repr(CarSerializer())
and seeing the field generated with read_only=True
set. You can override this with the extra_kwargs
Meta option which will allow you to override it and set read_only=False
.
class OwnerSerializer(serializers.ModelSerializer):
class Meta:
model = Owner
fields = ('id', 'name')
extra_kwargs = {
"id": {
"read_only": False,
"required": False,
},
}
This will include the id
field in the validated_data
when you need it.
Source:stackexchange.com