Search code examples
pythondjangodjango-filterdjango-filters

Django date-filter - Need month filter


I'm using django_filters to get the filter component. Objective is to have month and year filter component. The date can be set to 1 internally.

For example If I select May - 2016, then It should default to 1st of May 2016. In UI I should just select month and year.

I'm able to get some result using Date-Month-Year picker. However not the desired result.

 import django_filters as filters.

    class xyz(filters.FilterSet):
            schedule_date = filters.DateFromToRangeFilter(name='schedule_month',
                          widget=filters.widgets.RangeWidget(attrs={'class': 'datepicker'}))



    class Meta(object):
        form = forms.Form

I tried to use input_formats = ['%m-%Y'] -

schedule_date = filters.DateFromToRangeFilter(name='schedule_month',input_formats = ['%m-%Y'], 
widget=filters.widgets.RangeWidget(attrs={'class': 'datepicker'}))

But got an error TypeError: init() got an unexpected keyword argument 'input_format'

Any way to get this done? Any other widget say monthpicker rather than datepicker ?


Solution

  • I found a way. One way is to use attrs={'type': 'month'} That would give month picker. Not the great UI as it gives 3 options to set the value.