#33651: Support prefetch GenericForeignKey with custom queryset.
-------------------------------------+-------------------------------------
     Reporter:  elonzh               |                    Owner:
                                     |  Gullapalli Saisurya Revanth
         Type:  New feature          |                   Status:  assigned
    Component:                       |                  Version:  4.0
  contrib.contenttypes               |
     Severity:  Normal               |               Resolution:
     Keywords:  GenericForeignKey    |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by revanthgss):

 Hi,

 I think there is no way to add two lookups on same foreign key on same
 model as currently we populate the prefetch cache for this foreign key.
 Anyway, for a generic foreign key currently giving a queryset is not
 supported. To add this support I think the best way is to have a class
 that extends the prefetch class which handles all the custom logic of
 prefetching which is what we are working on in this issue.

 To implement what you said i.e adding two Prefetch of content object in
 prefetch_related function. Some of the logic will need to be handled in
 the prefetch_related function which is not good.

 Replying to [comment:19 Todor Velichkov]:
 > Hi everyone, I took a brief look at the conversation in the pull request
 [https://github.com/django/django/pull/15636 #15636]  and I have a
 question about the issue being discussed there (passing list of querysets
 into an queryset argument).
 >
 > Is there anything specific about this, that is enforcing the use of a
 list of querysets? Is it possible to send as many `GenericPrefetch`
 objects as the number of querysets being required ? For example:
 > {{{
 > Node.objects.prefetch_related(
 >     GenericPrefetch("content_object", queryset=ItemA.objects.all()),
 >     GenericPrefetch("content_object", queryset=ItemB.objects.all()),
 > )
 > }}}
 > this is in the spirit of the current `Prefetch` class, which actually
 allows you to prefetch multiple levels of objects, splitted into different
 prefetch calls. i.e:
 > {{{
 > Book.objects.prefetch_related(
 >     Prefetch("author", queryset=Author.objects.all()),
 >     Prefetch("author__house", queryset=House.objects.all()),
 > )
 > }}}
 > And now I'm a little bit repeating myself with ticket [ticket:24272],
 but this could probably open the door for reusing `related_query_name`
 when there is a defined `GenericRelation` which could give us the
 following interface w/o using the generic `content_object` argument:
 > {{{
 > TaggedItem.objects.all().prefetch_related(
 >     GenericPrefetch('books',
 queryset=Book.objects.all().select_related('author')),
 >     GenericPrefetch('movies',
 queryset=Movie.objects.all().select_related('director')),
 > )
 > }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33651#comment:20>
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/01070184a5b57d68-1f74011a-080c-4ba2-b441-c31422042de1-000000%40eu-central-1.amazonses.com.

Reply via email to