67π
β
I remember there being plans to add a __date
field lookup to make this easier, but as it stands the βstandardβ way of doing it is
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
Invoice.objects.get(user=user, date__range=(today_min, today_max))
π€Ismail Badawi
39π
You can also do something like this:
today = date.today()
invoice_for_today = Invoice.objects.filter(date__year=today.year, date__month=today.month, date__day=today.day)
π€Peppelorum
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Form field description in django admin
- [Django]-Why doesn't django's model.save() call full_clean()?
25π
in django<1.9
from django.utils.timezone import datetime #important if using timezones
today = datetime.today()
foo_for_today = Foo.objects.filter(datefield__year=today.year, datefield__month=today.month, datefield__day=today.day)
in django>1.9, as they added the date keyword
foo_for_today = Foo.objects.filter(datefield__date=datetime.date.today())
π€Marco Silva
- [Django]-How should I write tests for Forms in Django?
- [Django]-How to customize activate_url on django-allauth?
- [Django]-Loading initial data with Django 1.7+ and data migrations
- [Django]-Ordering admin.ModelAdmin objects in Django Admin
- [Django]-Python vs C#/.NET β what are the key differences to consider for using one to develop a large web application?
- [Django]-How do I clone a Django model instance object and save it to the database?
9π
To get entries from the Last 24 hours you can use:
from datetime import datetime, timedelta
Entry.objects.filter(pub_date__gte = datetime.now() - timedelta(days=1))
π€Gal Bracha
- [Django]-How to reload modules in django shell?
- [Django]-CSS styling in Django forms
- [Django]-Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
8π
There is a new __date field lookup in Django 1.9 you can use:
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
- [Django]-Example of Django Class-Based DeleteView
- [Django]-Django: Example of generic relations using the contenttypes framework?
- [Django]-Adding links to full change forms for inline items in django admin?
6π
Try using the keys date__gte
and date__lte
. You can pass in two datetime objects marking the boundaries of what you want to match.
π€jtbandes
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-Macros in django templates
- [Django]-Programmatically create a django group with permissions
Source:stackexchange.com