I am encountering an issue in Django when I try to do a PUT request in Postman.
I suspect the issue is because I have nested serializers in my model class, but I am not sure.
class Foo(models.Model):
foo_name = models.CharField(max_length=50, unique=True)
foo_statistics = models.ManyToManyField(TrainStatistics)
class FooSerializer(serializers.ModelSerializer):
blah_statistics = BlahStatisticsSerializer(many=True)
class Meta:
model = Foo
fields = ('foo_statistics')
depth = 2
def create(self, validated_data):
"""
Create and return a new `Summary` instance, given the validated data.
"""
blah_statistics_data = validated_data.pop('blah_statistics')
foo = Foo(**validated_data)
for blah_statistic in blah_statistics_data:
FooStatistic.objects.create(summary=summary, **train_statistic)
return summary
def update(self, instance, validated_data):
"""
Update and return an existing `Foo` instance, given the validated data.
"""
instance.blah_statistics = validated_data.get('blah_statistics', instance.blah_statistics) # This line is causing problems
Any idea what could be causing this issue?
The problem is that the validated data turns train_statistics
into a OrderedDict
(always it's a bit tricky working with nested serializers), so OrderedDicts
are unhashable, so when you try to ".get
" it raises an error.
An option is to set your field train_statistics
into read only.
Then in your update()
method, instead of using validated_data
to get train_statistics
, use request.data
for getting them. Do the same for create()
method.
class SummarySerializer(serializers.ModelSerializer):
train_statistics = TrainStatisticsSerializer(many=True, read_only=True)
class Meta:
model = Summary
fields = ('train_statistics')
depth = 2
def create(self, validated_data):
"""
Create and return a new `Summary` instance, given the validated data.
"""
request = self.context['request']
train_statistics_data = request.data.get('train_statistics')
summary = Summary.objects.create(**validated_data)
for train_statistic in train_statistics_data:
TrainStatistics.objects.create(summary=summary, **train_statistic)
return summary
def update(self, instance, validated_data):
"""
Update and return an existing `Summary` instance, given the validated data.
"""
request = self.context['request']
instance.train_statistics =request.data.get('train_statistics', instance.train_statistics) # This line is causing problems
When you call your serializer, you need to pass request object as context data.
SummarySerializer(instance, data=data, context={'request':request})
or
SummarySerializer(data=data, context={'request':request})