I override my FormSerializer's update method. It marks some nested field records as is_deprecated if necessary. However, the Api call returns all records, even though my get_queryset filters out everything is_deprecated. As far as I can tell, the get_queryset is being called to get the instance, which is then passed to the serializer. This instance does not contain any previously deprecated records.
After the update method completes, it looks like another query must be run that gets ALL of these nested records and serializes them. This only occurs on updates. This may be occurring when serializer is saved in UpdateModelMixin.update() when self.perform_update() is called.
I get the correct records on a standard GET request. Any idea where this second query is being called and how to override it? Alternatively, I should be able to edit the serialized data in to_representation, but this would be inefficient and I'd like to understand what is going on here first.
View
class FormViewSet(LoginRequiredMixin, viewsets.ModelViewSet):
serializer_class = FormSerializer
queryset = Form.objects.all()
def get_queryset(self):
if 'pk' in self.kwargs:
qs = Form.objects.filter(id=self.kwargs['pk'])
else:
qs = Form.objects.filter(id__in=name_dict.values()).order_by('name')
queryset = FormSerializer.eager_loading(qs)
return queryset
Serializer
class FormSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(required=False, allow_null=True)
fields = FieldSerializer(many=True)
class Meta:
model = Form
fields = '__all__'
@staticmethod
def eager_loading(queryset):
return queryset.prefetch_related(Prefetch('fields',queryset=Field.objects.filter(is_deprecated=False).order_by('field_order')))