daterange on a django-filter

just an edit for the current django version replace name with field_name, replace lookup_type with lookup_expr :

An example is given below for filters.py file

from django.contrib.auth.models import User
import django_filters

class UserFilter(django_filters.FilterSet):       
    username = django_filters.CharFilter(lookup_expr='icontains')
    first_name = django_filters.CharFilter(lookup_expr='icontains')
    last_name = django_filters.CharFilter(lookup_expr='icontains')
    year_joined = django_filters.NumberFilter(field_name='date_joined', lookup_expr='year',label='Year Joined')
    year_joined__gt = django_filters.NumberFilter(field_name='date_joined', lookup_expr='year__gt',label='Year Joined Greater Than')
    year_joined__lt = django_filters.NumberFilter(field_name='date_joined', lookup_expr='year__lt',label='Year Joined Less Than')
    date_range = django_filters.DateRangeFilter(field_name='date_joined')
    start_date = django_filters.DateFilter(field_name='date_joined',lookup_expr='lt',label='Date joined is before (mm/dd/yyyy):')
    end_date = django_filters.DateFilter(field_name='date_joined',lookup_expr='gt',label='Date joined is after (mm/dd/yyyy):')
    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', ] 

I can't speak to the Django REST Framework part of your question, but I hope I can lend some insight to the django-filter part!

If you use a DateRangeFilter, the result would be a dropdown with the options "any date", "today", "past 7 days", "this month" and "this year".

If your variable is 'date', either of these will give you a date range with the label "Date Range":

date_range = DateRangeFilter(field_name='date')
date = DateRangeFilter(label='Date_Range')

The difference is that if you use the first one, you can use your variable "date" again, so you could give your user the choice to filter by start date, end date, or both (to get dates between), all using the same date variable.

(You can only use "date" to the left of the equal sign one time (or not at all)).

To get the start and end date, do:

start_date = DateFilter(field_name='date',lookup_expr=('lt'),) 
end_date = DateFilter(field_name='date',lookup_expr=('gt'))

You don't need to call "date" again in Meta, but you can. You do need to call something. It could be your date, or some other field you want to filter.

Here is a full code and screenshot example:

from django_filters import DateRangeFilter,DateFilter
from wesapp.models import MyModel

class SaleItemFilter(django_filters.FilterSet):
    start_date = DateFilter(name='date',lookup_type=('gt'),) 
    end_date = DateFilter(name='date',lookup_type=('lt'))
    date_range = DateRangeFilter(name='date')

    class Meta:
        model = SaleItem
        fields = ['entered_by',]

enter image description here


You could also try the DateFromToRangeFilter This would enable you to do this:

f = F({'date_0': '2016-01-01', 'date_1': '2016-02-01'})

Where F is a FilterSet class with date = DateFromToRangeFilter

https://django-filter.readthedocs.io/en/stable/ref/widgets.html#rangewidget


I know this is old post but to give additional answer you can enable from and to datepicker also by the following method. Datepicker using django_filters.

import django_filters
from django_filters.widgets import RangeWidget

Date = django_filters.DateFromToRangeFilter(widget=RangeWidget(attrs={'type': 'date'}))