So here is my files - Models.py
class Model1(models.Model):
user = models.ForeignKey(User)
other_fields = models.CharField(max_length=40)
Serializers.py
class MySerializer(ModelSerializer):
class Meta:
model = Model1
fields = '__all__'
Here json request
{"user_id":1, "other_fields":"details"}
and in views.py
serializer = MySerializer(data=request.data)
serializer.data
**Throws keyerror "user"**
When i try to change request parameter "user_id" to "user" it works for me . But I can't change request json. Is there any way to handle this issue in serializer?
I can set all the fields inplace of '_all_' but it is not a good solution due to large number of fields .
I also tried with -
class MySerializer(ModelSerializer):
user = serializers.CharField(source='user_id')
class Meta:
model = Model1
fields = '__all__'
but it was not worked for me.
excludeparam in yourMetaclass, pass it a tuple of not required fields.Usermodel as well ?__all__will include all model fields + fields included explicitly in serializer