For a QuerySet
of blog entries, I want to create a DateQuerySet
of the months in which those posts were made. The query is:
dates = Entry.published.all().dates('pub_date', 'month')
"month" returns a list of all distinct year/month values for the field.
If I have 4 entries with dates :
- (2012, Feb, 3rd)
- (2012, Feb, 2nd)
- (2012, Jan, 24th)
- (2011, Dec, 28th)
I expect to get 3 datetime objects returned; one for Dec
, Jan
, Feb
, instead I get 4 returned, one for each of the original dates
Is this expected behaviour? I've tried adding distinct()
to the query, but it still returns every date.
UPDATE
A simple way to fix this is make a Set
from the DateQuerySet
:
dates = Entry.published.all().dates('pub_date', 'month')
return set(dates)
This removes the duplicates datetime
objects but I still don't understand why this is happening (or if I am misunderstanding how dates() works)
DateQuerySet
which is basically a list ofdatetime
objects. It returns one for every entry in the original queryset, regardless of whether or not the occur in the same month of not – Timmy O'Mahonypublished
manager inherits from the defaults and simply filters out Entries from the future – Timmy O'Mahony