#26602: Provide a way to manage grouping with RawSQL
-------------------------------------+-------------------------------------
Reporter: Jamie Cockburn | Owner: Manav
| Agarwal
Type: New feature | Status: assigned
Component: Database layer | Version: master
(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
-------------------------------------+-------------------------------------
Comment (by Manav Agarwal):
Replying to [comment:2 Josh Smeaton]:
> This happens because RawSQL defines:
>
> {{{
> def get_group_by_cols(self):
> return [self]
> }}}
>
> It'd be helpful if there was a way to disable grouping in an easy way.
For the moment, you should be able to do something like:
>
> {{{
> window = RawSQL("SUM(amount) OVER (ORDER BY created)", [])
> window.get_group_by_cols = lambda: []
> qs = A.objects.annotate(total=window)
> qs.count()
>
> # Or..
>
> RawAggregate(RawSQL):
> contains_aggregate = True # may not be necessary in newer django
versions
> def get_group_by_cols(self):
> return []
>
> qs = A.objects.annotate(total=RawAggregate("SUM(amount) OVER (ORDER BY
created)", []))
> }}}
>
> Can you let me know if this workaround helps you?
>
> In the meantime, I think it's worth figuring out how we want to treat
RawSQL when it should not be doing group bys, so I'm accepting on that
basis.
>
> Two options I see. First, we can define a whole new class as I've done
above - like RawAggregate. Second, we could use a kwarg to RawSQL to tweak
whether or not it contains aggregate code.
>
> {{{
> RawSQL('SELECT SUM() ..', [], grouping=False)
> }}}
I think we may use Josh's suggestion of adding a kwarg to RawSQL to tweak
whether or not it contains aggregate code. Please update if this is fine
so that I may submit a patch.
Regards
--
Ticket URL: <https://code.djangoproject.com/ticket/26602#comment:10>
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/064.a2a897fdcf2dead02a6a2e9ccbf5edfa%40djangoproject.com.