Package: django-admin-list-filters
DjangoAdminFilters libraryIn RussianThe free, open-source DjangoAdminFilters library is designed to filter objects in the Django admin site.
The library provide few filters for this purpose.MultiChoice: multi choice selection with checkboxes for CharField and IntegerField fields with 'choices' optionMultiChoiceExt: another version of previous filter, that allows filtering by custom defined propertiesDateRange: set a custom date range usinginputfieldsDateRangePicker: set a custom date range using javascript widget for select datetime from calendarMultiChoice and MultiChoiceExtDateRangeDateRangePickerFor javascript widget for DateRangePicker was used code fromdate-and-time-picker projectwith mergedpull request, that allow to select dates before current.Installationpipinstalldjango-admin-list-filtersTo connect library to your project, adddjango_admin_filtersto theINSTALLED_APPSlist  in yoursettings.pyfile.INSTALLED_APPS=(...'django_admin_filters',)Then connect the static files of the library.manage.pycollectstaticInitial dataLet's say we have a table in the database. The records contain follows fields.# models.pyfromdjango.dbimportmodelsSTATUS_CHOICES=(('P','Pending'),('A','Approved'),('R','Rejected'),)classLog(models.Model):text=models.CharField(max_length=100)timestamp1=models.DateTimeField(default=None,null=True)timestamp2=models.DateTimeField(default=None,null=True)status=models.CharField(max_length=1,default='P',choices=STATUS_CHOICES)is_online=models.BooleanField(default=False)is_trouble1=models.BooleanField(default=False)is_trouble2=models.BooleanField(default=False)Shared settings for all filters in the libraryYou can customize the appearance and behavior of filters to suit your needs by inheriting the filter classes from the library and overriding some of the attributes.
All library filters support the following attributes.fromdjango_admin_filtersimportMultiChoiceclassMyChoicesFilter(MultiChoice):FILTER_LABEL="Select options"BUTTON_LABEL="Apply"FILTER_LABEL: Filter titleBUTTON_LABEL: Title for filter apply buttonMultiChoice filterFor model fields of typeCharFieldorIntegerFielddefined using thechoicesparameter (for example, the 'status' field in theLogmodel), you can use the MultiChoice filter.
Values  of the parameterchoiceswill be displayed as checkboxes.To use MultiChoice filter, you need to specify them in theadmin.pyfile in thelist_filterattribute of the corresponding class.# admin.pyfromdjango.contribimportadminfromdjango_admin_filtersimportMultiChoicefrom.modelsimportLogclassStatusFilter(MultiChoice):FILTER_LABEL="By status"classAdmin(admin.ModelAdmin):list_display=['text','status']list_filter=[('status',StatusFilter)]admin.site.register(Log,Admin)In the Django admin panel, check the required checkboxes in the filter and click the "Apply" button.
If all filter checkboxes are unchecked and the apply filter button is pressed, than the filter will not been aplied and all records will be displayed.MultiChoiceExt filterSometimes you need to filter data by a custom defined property that does not match a single field in the model.For example, in theLogmodel of the source data, there are three boolean fields.is_online=models.BooleanField(default=False)is_trouble1=models.BooleanField(default=False)is_trouble2=models.BooleanField(default=False)For this model, we define thecolorproperty as follows.Thecolorproperty has the value 'red' if the fieldis_online == False.Ifis_online == Trueand bothis_trouble1andis_trouble1fields are False, then the value of the property is 'green'.Ifis_online == Trueand at least one of the fieldsis_trouble1andis_trouble1is True, then the property has the value 'yellow'.# models.py@propertydefcolor(self):status='red'ifself.is_online:status='green'ifself.is_trouble1orself.is_trouble2:status='yellow'returnstatusTo filter data by such a property in the Django admin panel, you can use the MultiChoiceExt filter.
In theoptionsattribute, you need to specify a list of checkboxes that will be displayed when using the filter.Each element of the list consists of three values.a unique string to be used in the GET request parametercheckbox labelfiltering expression applied to the DB model in the form ofDjango Q-objectsIn theparameter_nameattribute, you need to specify the name of the GET request parameter for sending filter data.For our example, the code will look like this.# admin.pyfromdjango.db.modelsimportQfromdjango_admin_filtersimportMultiChoiceExtclassColorFilter(MultiChoiceExt):FILTER_LABEL="By color"parameter_name="color"options=[('red','Red',Q(is_online=False)),('yellow','Yellow',Q(is_online=True)&(Q(is_trouble1=True)|Q(is_trouble2=True))),('green','Green',Q(is_online=True)&Q(is_trouble1=False)&Q(is_trouble2=False)),]classAdmin(admin.ModelAdmin):list_display=['text','color']list_filter=[ColorFilter]admin.site.register(Log,Admin)Otherwise, the behavior and settings of theMultiChoiceExtfilter are similar to theMultiChoicefilter described earlier.DateRange and DateRangePicker filtersTo use filters with a date interval, you need to specify them in theadmin.pyfile in thelist_filterattribute of the corresponding class.# admin.pyfromdjango.contribimportadminfromdjango_admin_filtersimportDateRange,DateRangePickerfrom.modelsimportLogclassAdmin(admin.ModelAdmin):list_display=['text','timestamp1','timestamp2']list_filter=(('timestamp1',DateRange),('timestamp2',DateRangePicker))admin.site.register(Log,Admin)Customization for DateRange filter# admin.pyfromdjango_admin_filtersimportDateRangeclassMyDateRange(DateRange):FILTER_LABEL="Data range"BUTTON_LABEL="Set range"FROM_LABEL="From"TO_LABEL="To"ALL_LABEL='All'CUSTOM_LABEL="custom range"NULL_LABEL="no date"DATE_FORMAT="YYYY-MM-DD HH:mm"is_null_option=Trueoptions=(('1da',"24 hours ahead",60*60*24),('1dp',"24 hours in the past",60*60*-24),)You can override the following attributes.FROM_LABEL: The label of the start date field.TO_LABEL: The label of the end date field.ALL_LABEL: The label of the menu item for displaying all records.CUSTOM_LABEL: The label of the menu item when date range is set.NULL_LABEL: The label of the menu item for displaying records without date.is_null_option: Set this attribute toFalseto remove the option to display record without date from the filter menu.DATE_FORMAT: Hint about the format of the date and time fields.You can change the date/time input format to your own.
However, you may need to override theto_dtimemethod as well.
This method is used to convert a user-entered string into adatetimevalue.
By default, the method is defined as follows.@staticmethoddefto_dtime(text):try:returndatetime.fromisoformat(text)exceptValueError:returnNoneTheoptionsattribute specifies filter menu items that allow you to select data from the current moment to an offset of a specified number of seconds in the past or future.
Each element of theoptionslist contains three values.A unique string to use in the GET request parameters. Except for the strings 'custom' and 'empty' which are used by the filter.The title of the item in the filter menu.Offset in seconds relative to the current moment. A negative value specifies an offset to the past.Customization for DateRangePicker filterTheDateRangePickerfilter with a javascript calendar date/time picker widget is derived from theDateRangefilter and allows you to override all the attributes described above.
Also, additional attributes can be overridden inDateRangePicker.# admin.pyfromdjango_admin_filtersimportDateRangePickerclassMyDateRangePicker(DateRangePicker):WIDGET_LOCALE='en'WIDGET_BUTTON_LABEL="Set"WIDGET_WITH_TIME=TrueWIDGET_START_TITLE='Start date'WIDGET_START_TOP=-350WIDGET_START_LEFT=-400WIDGET_END_TITLE='End date'WIDGET_END_TOP=-350WIDGET_END_LEFT=-400WIDGET_LOCALE: The language code for display the names of the months and days of the week. By default is the value of theLANGUAGE_CODEitem in your project'ssettings.pyfile.WIDGET_BUTTON_LABEL: The label of the select button.WIDGET_WITH_TIME: Set this attribute toFalseif you only want to select a date without a time.WIDGET_START_TITLE: The title of the widget when selecting the start date of the interval.WIDGET_START_TOP: The vertical offset of the widget's calendar window when selecting the start date of the interval.WIDGET_START_LEFT: The horizontal offset of the widget's calendar window when selecting the start date of the interval.WIDGET_END_TITLE: The title of the widget when selecting the end date of the interval.WIDGET_END_TOP: The vertical offset of the widget's calendar window when selecting the end date of the interval.WIDGET_END_LEFT: The horizontal offset of the widget's calendar window when selecting the end date of the interval.Usage exampleYou can run an example of using the library on your local host.On the Windows platform, you must first install the following programs.Python3GNUUnix Utilsfor operations via makefileGit for Windowsto access the source code repository.Then clone the repository and run the installation, specifying the path to Python 3.gitclonegit@github.com:vb64/django.admin.filters.gitcddjango.admin.filters
makesetupPYTHON_BIN=/usr/bin/python3Collect static files and create a database.makestatic
makedbCreate a database superuser by specifying a login and password for it.makesuperuserRun example.makeexampleOpenhttp://127.0.0.1:8000/admin/in a browser to view the example site.
To enter the admin panel you need to use the login and password that were set when creating the superuser.