-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ListSerializer.to_representation cuts data and KeyError is raised if the queryset used has been modified #2727
Comments
Seconded. This is currently causing some grief to me as well. @bakaat suggested this quick fix:
which "works" in terms of the data you get out, but isn't desirable for large querysets |
Indeed it's not. And it would possibly require you to rewrite all calls to serializer. While that's what I suggested, I actually worked around it by subclassing Serializer (We've got our own base class extending the ModelSerializer anyways)
Generator should be more efficient than list and still prevents reevalution. This said it still remains a |
@MattBlack85 (or anyone else) On further review I didn't fully understand this. |
Closing as a duplicate of #2704, but it'd be useful to know if there are other types of "extra data" along with |
@tomchristie In my situation it was something like
This actually causes the items in qs to be re-fetched from the db and |
Okay. That's (arguably) a slightly quirky thing to do, but it is at least a very simple demonstration of the issue! :) Thank you. |
Yup :) |
My case regards fetching data from another service, having then those data in a dict I was trying to update every object in my QS with that dictionary I got from the service: fetched_data = {'x': {'attr_a': 'some', ...},...}
qs = Model.objects.all().values()
for obj in qs:
obj.update(fetched_data.get(obj['id']), {}) almost the same |
Grand, thanks both. |
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:
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 sincedata.all()
wasn't called while passing QS around.The text was updated successfully, but these errors were encountered: