Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 450
Shahrukh Mohammad Avatar asked Jan 21 '26 15:01

Shahrukh Mohammad


2 Answers

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

or

Tasks.objects.all().order_by('datetime__minute')
like image 111
Exprator Avatar answered Jan 23 '26 20:01

Exprator


Task.objects.all().order_by('datetime__hour', 'datetime__minute')
like image 20
Kye Russell Avatar answered Jan 23 '26 21:01

Kye Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!