Opened 4 years ago

Closed 4 months ago

Last modified 4 months ago

#19222 closed Cleanup/optimization (fixed)

Clarify that custom managers don't apply to intermediate joins

Reported by: Andrew Badr Owned by: Yanik Koval
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Loic Bistuer)

I have a custom Manager to filter out model instances with where the field deleted is True. I'm using an ORM query that looks like user1.page_set.filter(membership__user=user2).

My expectation is that Membership objects with the deleted flag set are not included in the query. Instead, they are being included.

Change History (16)

comment:1 Changed 4 years ago by Matt Austin

Resolution: needsinfo
Status: newclosed
Type: UncategorizedBug

Attempted to reproduce this, but require more info on model relationships.

comment:2 Changed 3 years ago by jedediah

class BagelManager(Manager):
    use_for_related_fields = True

    def get_query_set(self):
        return super(BagelManager, self).get_query_set().filter(deleted=False)

class Bagel(Model):
    deleted = BooleanField()

    objects = BagelManager()
    all_bagels = Manager()

    def __str__(self):
        return "deleted" if self.deleted else "active"

class Customer(Model):
    bagels = ManyToManyField(Bagel)
>>> Bagel.objects.create(deleted=False)
<Bagel: active>
>>> Bagel.objects.create(deleted=True)
<Bagel: deleted>
>>> Bagel.all_bagels.all()
[<Bagel: active>, <Bagel: deleted>]
>>> Bagel.objects.all()
[<Bagel: active>]
>>> Bagel.objects.filter(deleted=True)
[]

Correct so far... deleted bagel is invisible through default manager

>>> c = Customer.objects.create()
>>> c.bagels.add(*Bagel.all_bagels.all())
>>> c.bagels.all()
[<Bagel: active>]
>>> c.bagels.filter(deleted=True)
[]

Still good... deleted bagel is invisible to related fields

>>> Customer.objects.filter(bagels__deleted=True)
[<Customer: Customer object>]

Here's the problem. The query join sees the deleted bagel. I would expect queries through relations to see the same data as the relations themselves.

This should either be fixed or documented as a known limitation.

comment:3 Changed 3 years ago by jedediah

Resolution: needsinfo
Status: closednew

comment:4 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I don't think this a behavior that's going to change, so documentation seems like the way to go. If you'd like to write a patch, I'll be happy to review it.

comment:5 Changed 7 months ago by Tim Graham

Keywords: use_for_related_fields added

comment:6 Changed 7 months ago by Loic Bistuer

Description: modified (diff)
Keywords: use_for_related_fields removed
Summary: Documentation for use_for_related_fields should clarify that it doesn't work for intermediate joinsClarify that custom managers don't apply to intermediate joins
Version: 1.4master

comment:7 Changed 6 months ago by Holly Becker

Easy pickings: set
Needs documentation: set
Owner: changed from nobody to Holly Becker
Status: newassigned

comment:8 Changed 5 months ago by Tim Graham

Has patch: set
Needs documentation: unset
Patch needs improvement: set

PR with comments for improvement.

comment:9 Changed 5 months ago by Holly Becker

Owner: Holly Becker deleted
Status: assignednew

comment:10 Changed 5 months ago by Tim Graham

Patch needs improvement: unset

comment:11 Changed 4 months ago by Yanik Koval

Owner: set to Yanik Koval
Status: newassigned

comment:12 Changed 4 months ago by Tim Graham

yanikkoval, not sure why you assigned yourself to the ticket. In case you didn't see it, we have a patch awaiting review linked in comment:8.

comment:13 Changed 4 months ago by Yanik Koval

Sorry, didn't notice.

comment:14 Changed 4 months ago by Tim Graham

Easy pickings: unset

I felt the existing proposal is more verbose than needed, so I created an alternate PR.

comment:15 Changed 4 months ago by GitHub <noreply@…>

Resolution: fixed
Status: assignedclosed

In 8fb53c50:

Fixed #19222 -- Documented that default managers aren't used for related queries.

comment:16 Changed 4 months ago by Tim Graham <timograham@…>

In 8df2da3:

[1.10.x] Fixed #19222 -- Documented that default managers aren't used for related queries.

Backport of 8fb53c50ce1c759c740960c9e1cef3cef39cabc5 from master

Note: See TracTickets for help on using tickets.
Back to Top