Repository navigation
Don't needlessly call .all() when following queryset relationships. - #2241
Conversation
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.
There was a problem hiding this comment.
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.
|
Either we need a test case for this, or your change is correct. Either way around there's work that needs doing here. |
|
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: 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? |
|
Done, If you need something new, please tell me :) |
|
Done |
|
Note to self - will probably refactor this into |
.all() when following queryset relationships.
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.