Re: [Django] #26010: I need to count objects in a queryset by year and month

2016-09-23 Thread Django
#26010: I need to count objects in a queryset by year and month
-+-
 Reporter:  Mahmood Khan |Owner:  nobody
 Type:  New feature  |   Status:  closed
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:  invalid
 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 Rich Rauenzahn):

 FYI:

 MonthTransform and YearTransform are gone now in Django 1.10.

 You can use TruncMonth/TrunchYear from django.db.models.functions.

 I believe it is slightly different -- I had to modify my html template to
 filter the value through |date: 'Y' and |date: 'm', otherwise I got
 something like "2016, midnight" rather than "2016"

 See:

 http://stackoverflow.com/questions/8746014/django-group-by-date-day-month-
 year
 https://code.djangoproject.com/ticket/26649
 https://docs.djangoproject.com/en/1.10/ref/models/database-
 functions/#trunc

--
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.bc4b70ca29c62bf087d1a91bc0f4da2e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #26010: I need to count objects in a queryset by year and month

2015-12-29 Thread Django
#26010: I need to count objects in a queryset by year and month
-+-
 Reporter:  mahmoodkhan  |Owner:  nobody
 Type:  New feature  |   Status:  closed
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:  invalid
 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 mahmoodkhan):

 Thanks! much appreciated.

 Replying to [comment:1 charettes]:
 > Hi mahmoodkhan,
 >
 > You don't `extra()` to achieve this. There's already a couple of open
 tickets that you can relate to (#25774 comes to mind) but you should
 annotate your queryset with a
 [https://docs.djangoproject.com/en/1.9/ref/models/lookups/#transform-
 reference Transform] in order to extract the year and months.
 >
 > If you don't want to write your own Transform subclass you can use the
 undocumented `MonthTransform` and `YearTransform` to annotate your
 queryset:
 >
 > {{{#!python
 > from django.db.models.lookups import MonthTransform as Month,
 YearTransform as Year
 >
 > Item.objects.annotate(
 > year=Year('date'),
 > month=Month('date'),
 > ).values('year', 'month').annotate(count=Count('pk'))
 > }}}
 >
 > Keep in mind that you're relying on a private API (used internally to
 implement the `__month` and `__year` lookups) that might be moved around
 or replaced.
 >
 > Once `values()` allows transforms and lookups you should be able to
 simply do this:
 >
 > {{{#!python
 > Item.objects.values('date__year',
 'date__month').annotate(count=Count('pk'))
 > }}}

--
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.9d1a8450c71ed4cdbff1c7bb426fe30d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #26010: I need to count objects in a queryset by year and month

2015-12-29 Thread Django
#26010: I need to count objects in a queryset by year and month
-+-
 Reporter:  mahmoodkhan  |Owner:  nobody
 Type:  New feature  |   Status:  closed
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:  invalid
 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
-+-
Changes (by charettes):

 * status:  new => closed
 * needs_better_patch:   => 0
 * component:  Uncategorized => Database layer (models, ORM)
 * needs_tests:   => 0
 * version:  1.9 => master
 * keywords:   => Queryset.extra
 * needs_docs:   => 0
 * resolution:   => invalid


Comment:

 Hi mahmoodkhan,

 You don't `extra()` to achieve this. There's already a couple of open
 tickets that you can relate to (#25774 comes to mind) but you should
 annotate your queryset with a
 [https://docs.djangoproject.com/en/1.9/ref/models/lookups/#transform-
 reference Transform] in order to extract the year and months.

 If you don't want to write your own Transform subclass you can use the
 undocumented `MonthTransform` and `YearTransform` to annotate your
 queryset:

 {{{#!python
 from django.db.models.lookups import MonthTransform as Month,
 YearTransform as Year

 Item.objects.annotate(
 year=Year('date'),
 month=Month('date'),
 ).values('year', 'month').annotate(count=Count('pk'))
 }}}

 Keep in mind that you're relying on a private API (used internally to
 implement the `__month` and `__year` lookups) that might be moved around
 or replaced.

 Once `values()` allows transforms and lookups you should be able to simply
 do this:

 {{{#!python
 Item.objects.values('date__year',
 'date__month').annotate(count=Count('pk'))
 }}}

--
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.1afcd9fa5e12fa9ed917d78bd507cdd3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.