简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM