Skip to content

ListSerializer.to_representation does not respect prefetches #2704

Closed

Description

@aleontiev

Update: Related to #2727 - @tomchristie


I noticed some undesirable behavior around ListSerializer's implementation of to_representation in the context of relations prefetched with Django 1.7's Prefetch object.

I am talking about this block of code:

        # Dealing with nested relationships, data can be a Manager,
        # so, first get a queryset from the Manager if needed
        iterable = data.all() if isinstance(data, (models.Manager, query.QuerySet)) else data
        return [
            self.child.to_representation(item) for item in iterable
        ]

To demonstrate the problem, consider a request made to fetch all auth.Users and their related auth.Groups matching a certain filter. This can be done with Prefetch like so:

>>> users_with_test_groups = User.objects.all().prefetch_related(Prefetch('groups', queryset=Group.objects.filter(name__icontains='test')

Lets look at the first user and his prefetched groups:

>>> user = users_with_test_groups[0]
>>> user.groups.all()
[<Group: test>]

... so far, so good. Now let's add DRF into the mix; I have a UserSerializer and a related GroupSerializer:

UserSerializer(ModelSerializer):
   groups = GroupSerializer(many=True)
   ...

GroupSerializer(ModelSerializer):
   ...   

If I call UserSerializer(users_with_test_groups, many=True).data, I expect to see my first user returned with only those related groups that contain test. But I actually see ALL groups related to that user!

This is because calling .all() on a filtered managed queryset will re-evaluate that queryset as if it has no filters:

>>> user.groups.all()
[<Group: test>]
>>> user.groups.all().all()
[<Group: student>, <Group: test>]

This took a while to figure out and seems like bizarre behavior. Why is calling .all() on a queryset necessary when you can just iterate over it?

As a workaround, I have started using a custom ListSerializer that never calls .all(). Interested to hear if anybody else has run into this or tried using DRF Serializers together with the Prefetch object.

Activity

  1. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    Closing this as duplicate of #2442.

  2. aleontiev commented on Mar 17, 2015

    @aleontiev
    ContributorAuthor

    @xordoquy this is actually a distinct issue that only comes up when you use the Prefetch object along with a queryset to filter the relation.

    My use case is providing a flexible GET API that provides filtering on secondary related resources.

  3. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    Will take time to investigate this further then

  4. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    This took a while to figure out and seems like bizarre behavior. Why is calling .all() on a queryset necessary when you can just iterate over it?

    This is to force the QS reevaluation when someone writes the view class such as:

    class View(...):
        queryset = User.objects.all()
    

    Which means that the queryset will be evaluated at the class declaration rather than on per request basis if we didn't force the reevaluation.

  5. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    @tomchristie do we really need the queryset reevaluation ?
    It definitively helps for new comers that don't fully understand QS but I'm a bit afraid we'll get a couple of similar issues - such as this one - at some point

  6. aleontiev commented on Mar 17, 2015

    @aleontiev
    ContributorAuthor

    This isn't a major blocker right now (e.g. we've been able to work around it with a custom ListSerializer and/or custom relation field that proxies a serializer)

    However, perhaps there should be an setting that allows you to toggle this behavior, which can be on by default for newcomers per your example?

  7. lovelydinosaur commented on Mar 17, 2015

    @lovelydinosaur
    Member

    @tomchristie do we really need the queryset reevaluation ?

    @xordoquy Sorry, which one specifically?

  8. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    @tomchristie the one we have in serializers:

            # Dealing with nested relationships, data can be a Manager,
            # so, first get a queryset from the Manager if needed
            iterable = data.all() if isinstance(data, (models.Manager, query.QuerySet)) else data
            return [
                self.child.to_representation(item) for item in iterable
            ]
    

    The more I think about it the more I believe it is encouraging bad patterns (i.e. queryset evaluated where they shouldn't be)

  9. lovelydinosaur commented on Mar 17, 2015

    @lovelydinosaur
    Member

    We certainly need it for the Manager case - I expect we do need it for the queryset case too, but it'd be worth look at the history for that line.

  10. kevin-brown commented on Mar 17, 2015

    @kevin-brown
    Contributor

    This is because calling .all() on a filtered managed queryset will re-evaluate that queryset as if it has no filters:

    Is this something that's documented? It seems strange that calling all() on a queryset multiple times will perform different queries.

  11. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    @kevin-brown adding .all() creates a new queryset which by default isn't evaluated hence the second query. In most cases the .all().all() won't add additional DB request since the first queryset won't get a chance to be evaluated.

  12. kevin-brown commented on Mar 17, 2015

    @kevin-brown
    Contributor

    My question is specifically about the different query. I understand that it should be re-evaluated, which would be fine if it triggered the same query, but I don't understand why it's being evaluated using a different query.

    I'm specifically referencing this

    >>> user.groups.all()
    [<Group: test>]
    >>> user.groups.all().all()
    [<Group: student>, <Group: test>]
    
  13. xordoquy commented on Mar 17, 2015

    @xordoquy
    Contributor

    @kevin-brown you probably have a point there. It should have been the very same request played another time. Probably an issue on the Django part.

  14. 2 remaining items

  15. lovelydinosaur commented on Jun 24, 2015

    @lovelydinosaur
    Member

    Have closed #2727 as a duplicate of this, although possible that we can more broadly state the issue, as it's probably not just prefetch_related that's at issue, but anything else that can be lost when .all() is called on the queryset.

  16. lovelydinosaur commented on Jun 24, 2015

    @lovelydinosaur
    Member

    TODO to progress this issue:

    • Issue a pull request with iterable = data.all() if isinstance(data, models.Manager) else data (Note that QuerySet is removed from the isinstance check.
    • Do any tests fail?
    • Check the blame/history on that line - when was it last modified and when was QuerySet added? What was the rationale at the time?

    Note that #2727 includes a very trival example for demonstrating the behavior that doesn't rely on prefetch_related... #2727 (comment)

  17. jpadilla commented on Jun 25, 2015

    @jpadilla
    Contributor

    Working on #3076 to hopefully progress this issue.

  18. added a commit that references this issue on Jun 25, 2015
  19. lovelydinosaur commented on Jun 25, 2015

    @lovelydinosaur
    Member

    Closed by #3076.

  20. added this to the milestone on Jun 25, 2015
  21. aleontiev commented on Jun 25, 2015

    @aleontiev
    ContributorAuthor

    Thanks for the fix guys :)

  22. lovelydinosaur commented on Jun 25, 2015

    @lovelydinosaur
    Member

    😄

  23. modified the milestones: , 3.2.0 Release on Jul 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions