Filter "Dates"

11 views
Skip to first unread message

Stephen Mizell

unread,
Mar 12, 2007, 3:37:10 PM3/12/07
to Django users
I'm trying my hardest to make an archive list on our website. I can
pull them up fine with Model.objects.dates('pub_date','month') but I
can't use .filter() on them for some reason. We add things to the
database sometimes with a pub_date > today's date so things are
automatically published in the future. Is there a way to use
something to filter these out?

Model.objects.dates('pub_date','month',
order='DESC').filter(pub_date__month__lte=date.month,
pub_date__year__lte=date.year)

Note: To explain the "lte" on month and year, we want new articles to
post the first day of each month. This is why I don't use now().

Thanks!

Stephen Mizell

Malcolm Tredinnick

unread,
Mar 12, 2007, 4:35:16 PM3/12/07
to django...@googlegroups.com

The .dates() methods returns a DateQuerySet, not a QuerySet. It's
intended to be the last thing in the sequence of methods that you call.
A DateQuerySet is a subset of QuerySet, but because it's had the query
slightly altered to return only the dates, it doesn't have the full
flexibility for further actions that a QuerySet does. That might well be
biting you here.

Try doing the filtering first (since filter() returns a QuerySet) and
then calling dates() on the result. So

Model.objects.filter(....).dates(....)

Remember that method calls are evaluated left to right, so you can't
just put them in any order if they return slightly different results.

Regards,
Malcolm


Stephen Mizell

unread,
Mar 12, 2007, 5:33:09 PM3/12/07
to Django users
> Try doing the filtering first (since filter() returns a QuerySet) and
> then calling dates() on the result. So
>
> Model.objects.filter(....).dates(....)

Worked perfectly. I would have bet money I had tried that earlier,
but it looks like I would have lost that bet. Thanks for your help
Malcolm!

Stephen Mizell

Reply all
Reply to author
Forward
0 new messages