I'm my use-case (see attached sample code), I'm wanting to query out
parent objects and then prefetch the child associated list (in this case
locations). Prefetching is able to pull through the child associated
list. My problem is that I need to constrain on an attribute that is
defined in the through table (record_status). I do not have the luxary of
being able to update my model, so I was looking for ways I could define my
prefetch so that it would return back the correct associated records, but
with the additional filter. The only way I could figure out how to make
it work, was to use the extra query. Anytime I tried to add filtering
logic in the prefetch that referenced CompanyLocation, I would get an
error about an invalid attribute. If I tried adding the criteria onto the
Company objects portion, it would not fail compilation, but would fail
querying due to multiple records being returned. It didn't make sense to
me to include it there anyway as I was not trying to constrain the Company
records, but the location records collection that was being prefetched.
Maybe there is a way of doing this that I didn't find. If not, I think it
would be a good use-case to be able to make references to the through
object in the prefetch itself.
--
Ticket URL: <https://code.djangoproject.com/ticket/34141>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "models.py" added.
* status: new => closed
* resolution: => invalid
Comment:
Please don't use this ticket tracker as a support channel
TicketClosingReasons/UseSupportChannels.
Just like any queryset the ones provided to `Prefetch` can be filtered
against reverse foreign keys.
In your case, since you've defined `CompanyLocation.location.related_name
= "companies"`, that means you can do
`filter(companies__record_status="Active')` to achieve the same result as
your `extra`. I suggest adding a more adequate name of
`CompanyLocation.location.related_name = "company_locations"` to avoid
ambiguity with the reverse relationship added to `Company.locations`.
--
Ticket URL: <https://code.djangoproject.com/ticket/34141#comment:1>