Shahrukh Mohammad
Shahrukh Mohammad

Reputation: 1105

Order by time of a Datetime field in django

I wanted to order a query set on the basis of a time of a datetime field. I have used the following (here Tasks is my model and datetime is the field)

Tasks.objects.all().order_by('datetime.time')

this doesn't work and also

Tasks.objects.all().order_by('datetime__time')

doesn't work as it is part of the same model. I tried using .annotate() but I don't know how exactly to do it.

How should I go about doing this?

Upvotes: 2

Views: 8561

Answers (2)

Kye
Kye

Reputation: 4495

Task.objects.all().order_by('datetime__hour', 'datetime__minute')

Upvotes: 2

Exprator
Exprator

Reputation: 27513

Tasks.objects.all().order_by('datetime__hour')

or

Tasks.objects.all().order_by('datetime__minute')

Upvotes: 7

Related Questions