ListSerializer.to_representation cuts data and KeyError is raised if the queryset used has been modified · Issue #2727 · encode/django-rest-framework (original) (raw)

@MattBlack85

I discovered this problem after upgrading to 3.1, after 86c5fa2 if a QuerySet is passed as iterable data.all() will be called. That's fine until we start to add extra data to the QS and use normal serializers for output.
Case:

objs = Model.objects.filter(x=y)
for obj in objs:
    obj.update(extra_dict)
serializer = MySerializer(objs, many=True)

serializer.data will contain only data from QS, ignoring totally the other portion of data added later and ending with a KeyError because we're missing fields that are declared on serializer, this wasn't happening before since data.all() wasn't called while passing QS around.