588👍
Use
Sample.objects.filter(date__range=["2011-01-01", "2011-01-31"])
Or if you are just trying to filter month wise:
Sample.objects.filter(date__year='2011',
date__month='01')
Edit
As Bernhard Vallant said, if you want a queryset which excludes the specified range ends
you should consider his solution, which utilizes gt/lt (greater-than/less-than).
305👍
You can use django’s filter
with datetime.date
objects:
import datetime
samples = Sample.objects.filter(sampledate__gte=datetime.date(2011, 1, 1),
sampledate__lte=datetime.date(2011, 1, 31))
- [Django]-Jquery template tags conflict with Django template!
- [Django]-Django py.test does not find settings module
- [Django]-Django Installed Apps Location
132👍
When doing django ranges with a filter make sure you know the difference between using a date object vs a datetime object. __range is inclusive on dates but if you use a datetime object for the end date it will not include the entries for that day if the time is not set.
from datetime import date, timedelta
startdate = date.today()
enddate = startdate + timedelta(days=6)
Sample.objects.filter(date__range=[startdate, enddate])
returns all entries from startdate to enddate including entries on those dates. Bad example since this is returning entries a week into the future, but you get the drift.
from datetime import datetime, timedelta
startdate = datetime.today()
enddate = startdate + timedelta(days=6)
Sample.objects.filter(date__range=[startdate, enddate])
will be missing 24 hours worth of entries depending on what the time for the date fields is set to.
- [Django]-How do you configure Django to send mail through Postfix?
- [Django]-Get the list of checkbox post in django views
- [Django]-Handle `post_save` signal in celery
30👍
You can get around the “impedance mismatch” caused by the lack of precision in the DateTimeField/date
object comparison — that can occur if using range — by using a datetime.timedelta to add a day to last date in the range. This works like:
start = date(2012, 12, 11)
end = date(2012, 12, 18)
new_end = end + datetime.timedelta(days=1)
ExampleModel.objects.filter(some_datetime_field__range=[start, new_end])
As discussed previously, without doing something like this, records are ignored on the last day.
Edited to avoid the use of datetime.combine
— seems more logical to stick with date instances when comparing against a DateTimeField
, instead of messing about with throwaway (and confusing) datetime
objects. See further explanation in comments below.
- [Django]-Django render_to_string missing information
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Is there a way to filter a queryset in the django admin?
12👍
you can use "__range"
for example :
from datetime import datetime
start_date=datetime(2009, 12, 30)
end_date=datetime(2020,12,30)
Sample.objects.filter(date__range=[start_date,end_date])
- [Django]-Django-reversion and related model
- [Django]-Why there are two process when i run python manage.py runserver
- [Django]-Nginx doesn't serve static
6👍
To make it more flexible, you can design a FilterBackend as below:
class AnalyticsFilterBackend(generic_filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
predicate = request.query_params # or request.data for POST
if predicate.get('from_date', None) is not None and predicate.get('to_date', None) is not None:
queryset = queryset.filter(your_date__range=(predicate['from_date'], predicate['to_date']))
if predicate.get('from_date', None) is not None and predicate.get('to_date', None) is None:
queryset = queryset.filter(your_date__gte=predicate['from_date'])
if predicate.get('to_date', None) is not None and predicate.get('from_date', None) is None:
queryset = queryset.filter(your_date__lte=predicate['to_date'])
return queryset
- [Django]-Why is __init__ module in django project loaded twice
- [Django]-A field with precision 10, scale 2 must round to an absolute value less than 10^8
- [Django]-How to specify an IP address with Django test client?
0👍
Is simple,
YourModel.objects.filter(YOUR_DATE_FIELD__date=timezone.now())
Works for me
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
0👍
Model
date = models.DateField()
View
def get_queryset(self):
fromDate = self.request.query_params.get('fromDate',None)
toDate = self.request.query_params.get('toDate',None)
response = yourModel.objects.filter(date__gte=fromDate,date__lte=toDate)
return response
- [Django]-How to tell if a task has already been queued in django-celery?
- [Django]-ImportError: Failed to import test module:
- [Django]-Are Django SECRET_KEY's per instance or per app?