My expectation is that `Membership` objects with the deleted flag set are
not included in the query. Instead, they are being included.
The documentation for `use_for_related_fields` should should specify that
it doesn't work in this case.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_better_patch: => 0
* resolution: => needsinfo
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
Comment:
Attempted to reproduce this, but require more info on model relationships.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:1>
Comment (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.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:2>
* status: closed => new
* resolution: needsinfo =>
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:3>
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:4>
* keywords: => use_for_related_fields
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:5>
* keywords: use_for_related_fields =>
* version: 1.4 => master
Old description:
> 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)`. The option
> `use_for_related_fields=True` is set on the relevant manager.
>
> My expectation is that `Membership` objects with the deleted flag set are
> not included in the query. Instead, they are being included.
>
> The documentation for `use_for_related_fields` should should specify that
> it doesn't work in this case.
New description:
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:6>
* status: new => assigned
* owner: nobody => Hwesta
* easy: 0 => 1
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:7>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* needs_docs: 1 => 0
Comment:
[https://github.com/django/django/pull/6902 PR] with comments for
improvement.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:8>
* owner: Hwesta =>
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:9>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:10>
* status: new => assigned
* owner: => yanikkoval
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:11>
Comment (by timgraham):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:12>
Comment (by yanikkoval):
Sorry, didn't notice.
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:13>
* easy: 1 => 0
Comment:
I felt the existing proposal is more verbose than needed, so I created an
alternate [https://github.com/django/django/pull/7074 PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"8fb53c50ce1c759c740960c9e1cef3cef39cabc5" 8fb53c50]:
{{{
#!CommitTicketReference repository=""
revision="8fb53c50ce1c759c740960c9e1cef3cef39cabc5"
Fixed #19222 -- Documented that default managers aren't used for related
queries.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:15>
Comment (by Tim Graham <timograham@…>):
In [changeset:"8df2da36379cfce0efc7f2da96fc076ed221dc0a" 8df2da3]:
{{{
#!CommitTicketReference repository=""
revision="8df2da36379cfce0efc7f2da96fc076ed221dc0a"
[1.10.x] Fixed #19222 -- Documented that default managers aren't used for
related queries.
Backport of 8fb53c50ce1c759c740960c9e1cef3cef39cabc5 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19222#comment:16>