Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django order by date in datetime / extract date from datetime

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?

like image 527
demux Avatar asked Sep 06 '10 16:09

demux


1 Answers

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')
like image 95
Manoj Govindan Avatar answered Nov 02 '22 21:11

Manoj Govindan