Skip to content

Don't needlessly call .all() when following queryset relationships. - #2241

Merged
lovelydinosaur merged 4 commits into
encode:masterfrom
IvanAlegre:patch-1
Dec 10, 2014
Merged

lovelydinosaur merged 4 commits into
encode:masterfrom
IvanAlegre:patch-1

Conversation

@IvanAlegre

Copy link
Copy Markdown
Contributor

Treat the input queryset as it comes (maybe it has been changed in a higher level). Evaluating .all() does nothing if or if not is a queryset.

Treat the input queryset as it comes (maybe it has been changed in a higher level). Evaluating .all() does nothing if or if not is a queryset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible we might be missing a test case for why this is needed?

When a serializer is applied as a child as eg. a many-to-many relationship, then the property will be a RelatedObjectManager (not a queryset) and I think we need .all() applying there.

Eg a1.publications.all() in the examples here.

Also possible that I'm being stupid and we don't actually need this at all.

@lovelydinosaur

Copy link
Copy Markdown
Member

Either we need a test case for this, or your change is correct. Either way around there's work that needs doing here.

@IvanAlegre

Copy link
Copy Markdown
Contributor Author

Yes, sorry for not providing test case. I didn't take into account that some child could be a many-to-many relationship. And in this case is needed the .all() invockation for retrieving the queryset.

In a project in which we are upgrading from djangoresframework 2.3.12 to 3.0.0 we have an issue related to this to_representation implementation. The issue is related to django parler, it needs to call set_current_language for every object in the queryset. So we have overriden the get_queryset method of a custom ModelViewSet in our views.py.

Inside the get_queryset we have:

bars = Bar.objects.all()
for bar in bars:
    bar.set_current_language('es-co')
return bars

So, when this is passed to the serializer, and the serializer calls a second time to .all() this ".set_current_language" calls are lost.

I've updated the Pull request. If the incoming data is a Manager, calls to .all(), if not (e.g. QuerySet) do not because is already a queryset and does not need to call to .all().

What do you think?
Thank you so much

@IvanAlegre

Copy link
Copy Markdown
Contributor Author

Done, If you need something new, please tell me :)

@IvanAlegre

Copy link
Copy Markdown
Contributor Author

Done

@lovelydinosaur

Copy link
Copy Markdown
Member

Note to self - will probably refactor this into get_attribute later.

lovelydinosaur added a commit that referenced this pull request Dec 10, 2014
@lovelydinosaur
lovelydinosaur merged commit fb313f8 into encode:master Dec 10, 2014
@lovelydinosaur lovelydinosaur changed the title Update serializers.py Don't needlessly call .all() when following queryset relationships. Dec 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants