I have the model
class Item(models.Model):
inicio = models.DateTimeField()
When I try to make this query:
itens = Item.objects.filter(inicio__hour__gte=6)
It returns me:
FieldError Unsupported lookup 'hour' for DateTimeField or join on the field not permitted.
How can I make this query?
You need to filter using a timedelta:
from datetime import datetime, timedelta
five_hours_ago = datetime.now() - timedelta(hours=5)
items = Item.objects.filter(inicio__lt=five_hours_ago)
You can always specify an exact datetime and then subtract 5 hours from it if you don't want 5 hours from the current datetime.
To the best of my knowledge, and according to the documentation, you can only do an exact lookup on an hour, minute or second.
Heads up, this should work as of Django 1.9
Event.objects.filter(timestamp__hour=23)
Event.objects.filter(time__hour=5)
Event.objects.filter(timestamp__hour__gte=12)