#26649: Group by date_trunc (day, month, year) without extra
-------------------------------------+-------------------------------------
     Reporter:  tback                |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     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
-------------------------------------+-------------------------------------

Comment (by jarshwah):

 Is the result you're after (not using your exact data, but the example
 should be clear enough):

 {{{
 month                count
 -----------------    ---------
 2016-01-01            13
 2016-02-01            24
 }}}

 If so, Django 1.10 introduces public APIs for Trunc:
 https://docs.djangoproject.com/en/1.10/ref/models/database-
 functions/#trunc

 Your query would now be something like:

 {{{
 from django.db.models.functions import TruncMonth
 Sales.objects
     .annotate(month=TruncMonth('timestamp'))  # Truncate to month and add
 to select list
     .values('month')                          # Group By month
     .annotate(c=Count('id'))                  # Select the count of the
 grouping
     .values('month', 'c')                     # (might be redundant,
 haven't tested) select month and count

 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26649#comment:2>
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/063.5e7d9050832fb132263c48d0373a82c7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to