Initialize a Django Filter with Date

Hi,

I have created a filter in Django - start date and end date, the filter works really well, but I’m wondering how can I initialize the filter with the current year, so the range is always 2022, but that going forward it always adjusts the start and end-date with the current year.

This is what I would like to get:

image

I know how to do this on the front end, for example, I can write a function in python and pass this variable to the front end, but I was wondering if there is a smarter way to do this using Django.

class LeadEntriesFilter(django_filters.FilterSet):
    start_date = DateFilter(field_name='date',
                                           widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
                                           lookup_expr='gt', label='Start Date')
    end_date = DateFilter(field_name='date',
                                         widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
                                         lookup_expr='lt', label='End Date')

Regards,
Francisco Colina

Hi Francisco!

I found this section at the django-filter docs and I think it’s important to consider the advice.

It is recommended that you do NOT implement the below as it adversely affects usability. Django forms don’t provide this behavior for a reason.

  • Using initial values as defaults is inconsistent with the behavior of Django forms.
  • Default values prevent users from filtering by empty values.
  • Default values prevent users from skipping that filter.

That said, I believe you can pass the value argument inside attrs. It’s just an HTML tag.
Also you can use min and max arguments.

Marco,

I probably should have explained myself a bit better, because I actually just want to populate the filter in the front-end but I don’t want to apply it, essentially, the consumers of the app want to have always the current year in the filter (to avoid typing), but they still want to see the full period of time, so it seems like a front-end job.

When you say the consumers of the app want to have always the current year in the filter, do you mean to have the values 1/1/2022 - 12/31/2022 as selected values in the filters, so they can click the Search button and filter on that period?

Yes, exactly that, so I thought there was a way to initialize the field value using Django forms, so they can click the search button

I think something like this could do it.

class LeadEntriesFilter(django_filters.FilterSet):
    start_date = DateFilter(
        field_name='date',
        widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date', value="2021/01/01"}),
        lookup_expr='gt', label='Start Date'
    )
    end_date = DateFilter(
       field_name='date',
       widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date', value="2021/12/31"}),
       lookup_expr='lt', label='End Date'
    )

I’m not able to test this right now so I’m not 100% sure. You could try passing the value arg directly to DateFilter.

DateFilter(
       field_name='date',
       widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
       lookup_expr='lt', label='End Date',
       value=date(2021, 12, 31)
    )

Marco, Thanks

I’m getting an error:

TypeError: init() got an unexpected keyword argument ‘value’

I have tried a string and it doesn’t work either.

My bad, filters don’t receive value as an argument.

Did you try adding it inside attrs like this?

widget= forms.DateInput(attrs={'class': 'form-control', 'type': 'date', value="2021/01/01"}),