Denis,
I tried to use get_serializer_context for my project, but I got a bit confused, since the documentation you proposed me was for method-views. Then I found
this link explaining how I can override get_serializer_context for GenericViews and tried to apply it on my case, with no luck. Here is my scenario:
My models:
class Report(models.Model):
name = models.CharField(max_length=150, blank=True, null=True)
year = models.DateField(blank=True, null=True)
is_final = models.BooleanField(blank=True, default=False, db_index=True)
class Section(models.Model):
name = models.CharField(max_length=500)
text = models.Text(blank=True, null=True)
report = models.ForeignKey(Report, related_name='sections')
class Data(models.Model):
name = models.CharField(max_length=150)
text = models.ForeignKey(RichText, blank=True, null=True, related_name='%(app_label)s_%(class)s_start_text')
section = models.ForeignKey(Section, related_name='data')
saved = models.BooleanField(default=False, db_index=True)
So, in short, I have report objects that have associated sections to them and each section has associated data.
My views are as follows:
class ReportViewSet(viewsets.ModelViewSet):
queryset = Report.objects.all()
serializer_class = serializers.ReportSerializer
class SectionViewSet(viewsets.ModelViewSet):
queryset = Section.objects.all()
serializer_class = serializers.SectionSerializer
class DataViewSet(viewsets.ModelViewSet):
queryset = Data.objects.all()
serializer_class = serializers.DataSerializer
And my serializers are as follows:
class DataSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = EmissionGroup
class SectionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Section
class ReportSerializer(serializers.HyperlinkedModelSerializer):
sections = SectionSerializer(many=True, read_only=True)
class Meta:
model = Report
What I am trying to do is to add an extra boolean field on my SectionSerializer (named, eg. all_saved), whose value will be True if all its related Data.saved values are also True.
When I tried to override get_serializer_context on SectionViewSet to test how it works, I've made the following changes:
class SectionViewSet(viewsets.ModelViewSet):
queryset = Section.objects.all()
serializer_class = serializers.SectionSerializer
def get_serializer_context(self): context = super(SectionViewSet, self).get_serializer_context()
context.update({'x': '11'})
return context
class SectionSerializer(serializers.HyperlinkedModelSerializer):
all_saved = serializers.SerializerMethodField('get_x')
def get_x(self, instance):
return self.context['xx']
class Meta:
model = Section
Then when I was viewing /section/ everything worked as expected, but when I was viewing /report/ I was getting the error:
KeyError at /report/
'x'
Request Method: GET
Django Version: 1.6.1
Exception Type: KeyError
Exception Value: 'x'
Exception Location: /path/to/serializers.py in get_x, line 129
And the truth is that SectionViewSet.get_serializer_context() never gets called (I can infer this since my debugger never stops at a break-point that I am placing in this function).
What is the correct way of using additional context in my case?
Thanx again for your time!