0

I have model like this:

class Order(models.Model):
    dateTime = models.DateTimeField()

and I want to get object with specific hour how can I do that? the code below doesn't work:

o=Order.objects.get(dateTime.hour=12)

and has this problem: keyword can't be an expression

now.. How should I give the order object with specific time?

Happy Developer
  • 617
  • 1
  • 8
  • 15

2 Answers2

1

The following will give you all the objects having hour value as 12.

o = Order.objects.filter(dateTime__hour=12)

which can be used in place of

o = Order.objects.get(dateTime__hour=12)` 

to get that one object, in case you have unique hour values for objects.

But if already know that you have unique value of hour then you should use the later.

Ankush Raghuvanshi
  • 1,392
  • 11
  • 17
0

https://docs.djangoproject.com/en/1.9/ref/models/querysets/#hour

o = Order.objects.filter(dateTime__hour=12)
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223