#34141: Prefetch querysets access to parent query through table
-----------------------------------------+--------------------------------
               Reporter:  nixj14         |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  4.1
               Severity:  Normal         |       Keywords:  QuerySet.extra
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+--------------------------------
 In working on a project recently, I ran into the following use-case:
 1) A parent model exists with a ManyToMany field.
 2) This field goes through a ManyToMany table.
 3) The ManyToMany table has an attribute that I'm interested in filtering
 on.

 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701844395f7cb-45d90159-fe01-4484-80d8-816ae4533ee4-000000%40eu-central-1.amazonses.com.

Reply via email to