Reputation: 2738
I am trying to find a straight forward way of doing filer based queries based on a datetimefiled. I have tried things like
timeDiff = datetime.datetime.now(pytz.utc) - latest_order.date
days = timeDiff.days
if days <= 2:
but it always gives me RuntimeWarning: DateTimeField received a naive date time
warning. Is there a better way of exciting these queries? How can I do for instance, filer and select all objects that were created more than 48 hours ago?
Would something like this will be the correct way of doing it?
example = Example_Request.objects.filter(date_created__gte=datetime.datetime.today()-datetime.timedelta(days=2))
Upvotes: 0
Views: 2342
Reputation: 11
To avoid the naive date time warning, you may want to switch out:
datetime.now()
for:
timezone.now()
Upvotes: 1
Reputation: 3981
Yes, the query you posted is the correct way of doing it. However, if you want to get objects created more than 48 hours ago, you'll want to change it to date_created__lte
. Also, you can pass in hours into a timedelta
. For example: datetime.timedelta(hours=48)
.
Relevant links:
Upvotes: 5