#26426: Add a way to customize a QuerySet's joins
-------------------------------------+-------------------------------------
     Reporter:  yourcelf             |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  1.9
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  QuerySet.extra       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by timgraham):

 * stage:  Unreviewed => Accepted


Old description:

> This ticket is just to document a use case for QuerySet.extra as
> requested by the docs:
> https://docs.djangoproject.com/en/1.9/ref/models/querysets/#extra
>
> I have a Category model like this:
>
> {{{
> class Category(models.Model):
>     name = models.CharField(max_length=200)
>     followers = models.ManyToManyField(User)
> }}}
>
> I want to get a list of all categories, but to annotate each category
> with whether the currently logged in user is a "follower" of the
> category.  Neither `prefetch_related` nor `annotate` work here, because I
> don't want to fetch nor aggregate over //all// "followers" (potentially
> many), I just want the presence of the current user.  The extra query
> looks like this:
>
> {{{
> Category.objects.filter(...).extra(
>     select={'is_following': '''EXISTS(
>         SELECT "id" FROM "projects_category_followers" WHERE
> "projects_category_followers"."category_id"="projects_category"."id" AND
>             "projects_category_followers"."user_id"=%s
>     )'''},
>     select_params=(request.user.id,)
> )
> }}}

New description:

 This ticket is just to document a use case for QuerySet.extra as requested
 by the docs:
 https://docs.djangoproject.com/en/1.9/ref/models/querysets/#extra

 I have a Category model like this:

 {{{
 class Category(models.Model):
     name = models.CharField(max_length=200)
     followers = models.ManyToManyField(User)
 }}}

 I want to get a list of all categories, but to annotate each category with
 whether the currently logged in user is a "follower" of the category.
 Neither `prefetch_related` nor `annotate` work here, because I don't want
 to fetch nor aggregate over //all// "followers" (potentially many), I just
 want the presence of the current user.  The extra query looks like this:

 {{{
 Category.objects.filter(...).extra(
     select={'is_following': '''EXISTS(
         SELECT "id" FROM "projects_category_followers" WHERE
 "projects_category_followers"."category_id"="projects_category"."id" AND
             "projects_category_followers"."user_id"=%s
     )'''},
     select_params=(request.user.id,)
 )
 }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26426#comment:4>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.e2963cdb9b28ea175abbffb842540875%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to