Regis Santos
Regis Santos

Reputation: 3749

Order by Datefield only in Django

I want to sort by DateTimeField ignoring one hour. Because in my ordination I need

Person.objects.order_by('-date_joined', 'full_name')

Sorting by name has no effect because it is a Timefield , but I wanted to data.

Upvotes: 2

Views: 1329

Answers (1)

Todor
Todor

Reputation: 16050

Depending on your Django version (1.9+), this should work:

Person.objects.order_by ('-date_joined__date','full_name')

Otherwise you can use .extra to cast into date field:

Person.objects.extra(
    select={'joined_date': 'DATE(myapp_person.date_joined)'},
    order_by=['-joined_date', 'full_name'],
)

Upvotes: 4

Related Questions