Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatetimeField filter after a given date

Is it possible to filter over a DateTimeField to get all objects who have a DateTimeField later to the given date ? For instance :

class MyObject(models.Model):
    updated_date = models.DateTimeField()

in a view :

given_date = someDateTime
new_items = MyObject.objects.filter(updated_date__after = given_date)
like image 704
Kobz Avatar asked Apr 22 '14 16:04

Kobz


1 Answers

Use __gt in a double underscore notation:

new_items = MyObject.objects.filter(updated_date__gt=given_date)

Relevant threads:

  • Django database query: How to filter objects by date range?
  • Django query datetime for objects older than 5 hours
  • Using datetime to compare with dates in Django
like image 121
alecxe Avatar answered Oct 25 '22 03:10

alecxe