I am trying to build a query for a view in Django in which I want to retrieve rows with today date (no matter the time).
I was thinking in a range between current date and datetime.datetime.now()
However I can't get only the date but not the time.
I have this:
now = datetime.datetime.now()
today = datetime.datetime.today()
var = Example.objects.filter(date__gt=datetime.date(today.year(), today.month(), today.day()), fecha__lt=now)
Working with Filter Easily the most important method when working with Django models and the underlying QuerySets is the filter() method, which allows you to generate a QuerySet of objects that match a particular set of filtered parameters.
Django-filter is a generic, reusable application to alleviate writing some of the more mundane bits of view code. Specifically, it allows users to filter down a queryset based on a model's fields, displaying the form to let them do this. Adding a FilterSet with filterset_class. Using the filterset_fields shortcut.
today = datetime.datetime.today()
Example.objects.filter(
date__year=today.year,
date__month=today.month,
date__day=today.day
)
Grab rows that have a create date greater than the current date using only the date() method of the datetime.now() class.
where row.createdate > datetime.datetime.now().date()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With