views:

30

answers:

1

I have a model with a datetime field and I want to show the most viewed entries for the day today.

I thought I might try something like dt_published__date to extract the date from the datetime field but obviously it didn't work.

popular = Entry.objects.filter(type='A', is_public=True).order_by('-dt_published__date', '-views', '-dt_written', 'headline')[0:5]

How can I do this?

+2  A: 

AFAIK the __date syntax is not supported yet by Django. There is a ticket open for this.

If your database has a function to extract date part then you can do this:

popular = Entry.objects.filter(**conditions).extra(select = 
    {'custom_dt': 'to_date(dt_published)'}).order_by('-custom_dt')
Manoj Govindan
date(dt_published) does the trick for mysql, thanks
Arnar Yngvason
But won't this slow down my query?
Arnar Yngvason