Package: django-advanced-filters
Django Advanced FiltersBranchBuildCoveragePyPIGitterMasterDevelopA django ModelAdmin mixin which adds advanced filtering abilities to the
admin.Mimics the advanced search feature inVTiger,see here for more
infoFor release notes, seeChangelogRequirementsDjango 2.2, >= 3.2 on Python 3.6+/PyPy3simplejson >= 3.6.5, < 4Installation & Set upInstall from pypi:pip installdjango-advanced-filtersAdd'advanced_filters'toINSTALLED_APPS.Addurl(r'^advanced_filters/',include('advanced_filters.urls'))to your project’s urlconf.Runpython manage.py syncdborpython manage.py migrate(for django >= 1.7)Integration ExampleExtending a ModelAdmin is pretty straightforward:fromadvanced_filters.adminimportAdminAdvancedFiltersMixinclassProfileAdmin(AdminAdvancedFiltersMixin,models.ModelAdmin):list_filter=('name','language','ts')# simple list filters# specify which fields can be selected in the advanced filter# creation formadvanced_filter_fields=('name','language','ts',# even use related fields as lookup fields'country__name','posts__title','comments__content',)Adding a new advanced filter (see below) will display a new list filter
named “Advanced filters” which will list all the filter the currently
logged in user is allowed to use (by default only those he/she created).Custom naming of fieldsInitially, each field inadvanced_filter_fieldsis resolved into an
actual model field. That field’s verbose_name attribute is then used as
the text of the displayed option. While uncommon, it occasionally makes
sense to use a custom name, especially when following a relationship, as
the context then changes.For example, when a profile admin allows filtering by a user name as
well as a sales representative name, it’ll get confusing:classProfileAdmin(AdminAdvancedFiltersMixin,models.ModelAdmin):advanced_filter_fields=('name','sales_rep__name')In this case the field options will both be named “name” (by default).To fix this, use custom naming:classProfileAdmin(AdminAdvancedFiltersMixin,models.ModelAdmin):advanced_filter_fields=('name',('sales_rep__name','assigned rep'))Now, you will get two options, “name” and “assigned rep”.Adding new advanced filtersBy default the mixin uses a template which extends django’s built-inchange_listtemplate. This template is based off of grapelli’s fork
of this template (hence the ‘grp’ classes and funny looking javascript).The default template also uses the superbmagnificPopupwhich is currently bundled
with the application.Regardless of the above, you can easily write your own template which
uses context variables{{ advanced_filters }}and{{ advanced_filters.formset }}, to render the advanced filter
creation form.StructureEach advanced filter has only a couple of required fields when
constructed with the form; namely the title and a formset (consisting of
a form for each sub-query or rule of the filter query).Each form in the formset requires the following fields:field,operator,valueAnd allows the optionalnegateandremovefields.Let us go over each of the fields in a rule fieldset.FieldThe list of all available fields for this specific instance of the
ModelAdmin as specific by the`advanced_filter_fieldsproperty. <#integration-example>`__The OR fieldORis an additional field that is added to every rule’s available
fields.It allows constructing queries withOR
statements.
You can use it by creating an “empty” rule with this field “between” a
set of 1 or more rules.OperatorQuery field suffixes which specify how theWHEREquery will be
constructed.The currently supported are as follows:iexact,icontains,iregex,range,isnull,istrueandisfalseFor more detail on what they mean and how they function, see django’sdocumentation on field
lookups.ValueThe value which the specific sub-query will be looking for, i.e the
value of the field specified above, or in django query syntax:.filter(field=value)NegateA boolean (check-box) field to specify whether this rule is to be
negated, effectively making it a “exclude” sub-query.RemoveSimilarly to otherdjango
formsets,
used to remove the selected line on submit.Editing previously created advanced filtersTheAdvancedFilterAdminclass (a subclass ofModelAdmin) is
provided and registered withAdvancedFilterin admin.py module.The model’s change_form template is overridden from grapelli’s/django’s
standard template, to mirror the add form modal as closely as possible.Note:currently, adding new filters from the ModelAdmin change page is
not supported.Query SerializationTODO:write a few words on how serialization of queries is done.Model correlationSince version 1.0,AdvancedFilterare tightly coupled with a specific model
using themodelfield and the app_label.Name template.
On creation,modelis populated based on the admin changelist it’s created
in.This change has a few benefits:The mixin can be used with multipleModelAdminclasses while
performing specific query serialization and field validation that are
at the base of the filter functionality.Users can edit previously created filters outside of the
context of a changelist, as we do in the`AdvancedFilterAdmin<#editing-previously-created-advanced-filters>`__.Limit theAdvancedListFiltersto limit queryset (and thus, the
underlying options) to a specified model.ViewsThe GetFieldChoices view is required to dynamically (using javascript)
fetch a list of valid field choices when creating/changing anAdvancedFilter.TODOAdd permission user/group selection functionality to the filter formAllow toggling of predefined templates (grappelli / vanilla django
admin), and front-end features.Support more (newer) python/django versionsChangelog2.0.0 - Support Django 3.2 and 4.0BREAKING CHANGE:This release is the first 2.x release, and drops support for EOL python and Django versions, all feature development will be done against 2.X branch.**Changes since 1.4.0:FeaturesAdd support for python 3.10 and Django 4.0 (Merge 7bfb5b6)Add compiled IT translation (Merge e39395f)Bug fixesDon’t add empty form to AdvancedFilterFormSet.forms (Merge 7bfb5b6)OtherDrop support for EOL Python 2.7 and 3.5 (Merge dfeb005)Drop support for EOL Django 3.0 (Merge dfeb005)Drop support for EOL Django up to 2.2 (Merge dfeb005)Upgrade Python syntax with pyupgrade –py36-plus (Merge dfeb005)Remove six (Merge dfeb005)Remove unused import (Merge dfeb005)Drop support for python 3.6 (Merge 7bfb5b6)Correct support matrix (Merge 7bfb5b6)Simplify url path import (Merge 7bfb5b6)Remove standalone clean env from tox envlist (Merge 7bfb5b6)Remove unused cached_property import (Merge 7bfb5b6)Add Django 3.2 to classifiers (#163)f-string for model_name string interpolation (Merge dfeb005)remove unsupported django 3.1 from tox matrix (Merge 7bfb5b6)update README and remove Django 3.1 classifierContributorsFabrizio CoralliniDmytro LitvinovHugo van KemenadePavel Savchenko1.4.0 - Latvian translation and minor fixesNOTE: This release will be the last one to include features in 1.X branch, other than urgent hotfixes; version 2.X will drop support for EOL python and Django versions, and all future development will be done against 2.X branchChanges since 1.3.0:FeaturesAdd Latvian translation (#135)Commit compiled translation files (Merge db448fa)Bug fixesswitch from ugettext to gettext (#134)don’t use default dict (Merge db448fa)correctly update extra_context (Merge db448fa)support parallel coverage reporting (Merge db448fa)OtherFix CI for 2021 (#147)Don’t run tests in GitHub actions twice, one per PR is enough (da6fe7f24982a0fa69fcebff79d658b087de5e5b)Ignore vscode settings (Merge db448fa)ContributorsPavel SavchenkoMāris Nartišs1.3.0 - Django 3.1 and moreApologies for the late release, and thanks to everyone that contributed.Bug fixesAdd support for python 3.9 and django 3.1import FieldDoesNotExist from django.core.exceptionsMiscUpdate django-bracesUpdate Admin to show modelAdd Turkish translationCorrect travis.yml deprecated/dupe keywordsContributorsPavel SavchenkopredatellÖzcan YARIMDÜNYAThu Trang PhamJoão Batista1.2.0 - Django 3 and moreIt’s finally time to drop the dirty old rags and don some fresh colors.Thanks to effort from multiple contributors, this version includes support
for newest Django version.Breaking ChangesAdd support for Django 2.2 and 3.0Drop support for Django < 1.9Drop support for Python 3.3-3.4django-advanced-filters now support onlypython 2.7, and 3.5 - 3.8.FeaturesSwitch deprecated force_text to force_str (Merge 0427d11)Bug fixesAvoid installing newer braces (Merge 0427d11)Allow choices sort on None fields (Merge 142ecd0)Docs / TestsUpdate dependencies stated in the READMERefactor some unittest test cases into pytest (Merge 41271b7)Test the CleanWhiteSpacesMixin helperMiscUpdate requirements for new test deps matrix (Merge 0427d11)Replace deprecated assertEquals (Merge 41271b7)Replace deprecated logger.warn with warning (Merge 41271b7)Bump test dependencies (Merge 41271b7)Update python and add Django classifiersContributorsPetr DlouhýAlon RaizmanHugo MaingonnatArpitPavel Savchenko1.1.1 - CHANGELOG rendering is hardThis release is for fixing the bug when installing with specific environment (
locale that defaults to CP-1252).Bug fixesAdd encoding=’utf-8’ to open() in setup.py (Merge 2fe81aa)Docs / Otheradd CONTRIBUTING.rst with common processes (Merge ee7907e)Update issue templates (Merge ee7907e)ContributorsRebecca TurnerPavel Savchenko1.1.0 - The future is brightThis release highlights support for Django 2.0 and 2.1 as well as
deprecating support for versions Django < 1.7 and Python 2.6 and 3.3Bug fixesbump django-braces==1.13 for Django 2 support (Merge 80e055e)use request context processor in test_project (Merge 80e055e)Misc.ignore .DS_Storefixes for Django 2.0 and 1.11, update tests (Merge 80e055e)test in Django 2.1 (Merge d8d236d)add updated migrations of model attributes (Merge 80e055e)fix ValueError while creating empty form (Merge d8d236d)python 2.6 and django < 1.7 are deprecatedlower and upper bounds in install_requiresavoid all-catch except clause (Merge 80e055e)Testscorrect tox env django spec for ver 1.11 (Merge 80e055e)correct make_query assertion for Django>=2 (Merge 80e055e)update pytest-django in diff. envs + tox (Merge d8d236d)ContributorsGoncalo GomespredatellPetr Dlouhýbenny daonPavel Savchenko1.0.7.1 - Fix PyPi failEquivalent to the prev version, bumped since we can’t reupload the files to PyPi.1.0.7 - The holiday editionThis is mostly a minor release with the biggest being theAdvancedFilterForm.Mediafix, 2 additional translations and bunch of docs cleanup (thanks everyone)!Changes since 1.0.6:Bug FixesFix AdvancedFilterForm Media declarationFix pep8: E128 on forms.py (Merge d7acb36)FeaturesAdd Japanese locale (Merge d7acb36)Add Spanish locale (Merge 1a482cf)Documentation:a bit of polishing (Merge 4c88ea3)removing confusing migrations paragraph (Merge 4c88ea3)Contributors:KINOSHITA ShinjiPavel SavchenkoBenny DaonMathieu RichardozJosé Sánchez Moreno1.0.6 - Bout TimeThis release is long overdue, and includes some important fixes as well as general improvements to code and documentation.Bug Fixesfixing TypeError: can only concatenate tuple (not “list”) to tupleensure select2 is included last (Merge 9831ba5)add script to load jQuery globallyremove invalid template variablesfix input focusing error in chromefix error when one missing range parameter caused error + test (Merge 365b646)Featuresdon’t override original change_list_templates in AdminAdvancedFiltersMixinmake date range placeholder more pleasant (Merge 365b646)add created_at fieldRussian locale providedDocumentationmake it clear easy-select2 is not required anymore (Merge 9831ba5)Clarify how to import AdminAdvancedFiltersMixin in READMETestsadd more fields/filter to test ModelAdminContributorsGrigoriy BeziukНикита КонинPavel SavchenkoYuval AdamPetr Dlouhý1.0.5 - Compatibility bumpBugsupdated AdvancedFilterQueryForm to include numeric comparison operators (Merge d3ee9f4)Fixed a bug where editing an existing Advanced Filter defaulted all operators to ‘Equals’ (Merge d3ee9f4)set AFQFormSet extra=0 instead of extra=1. I did this because having to check Delete is not clear to end users. (Merge d3ee9f4)changed the Advanced Filter admin so you a User by default can only view/edit filters that they create (unless they are a superuser) (Merge d3ee9f4)Fixed failing tests. Fixed bug where users weren’t properly getting permissions to change or delete their filters (Merge d3ee9f4)changed solution for extra form appearing on editing. Now initialize form checks for falsy value for extra rather than extra just being None (Merge d3ee9f4)removed ‘not instance from requirements for no extras (Merge d3ee9f4)pep8 fix (Merge d3ee9f4)Fixed labeling error with ‘Greater Than or Equal To’ (Merge d3ee9f4)Changes URL declaration to avoid deprecated patternselect2 only initializes if there are choices available. otherwise, the standard text input will be used (Merge 35d7063)Revert “select2 only initializes if there are choices available. otherwise, the standard text input will be used” (Merge 35d7063)updated query for choices for select2 field so that it will take only distinct choices. This allows max_choices to be the maximum unique choices. (Merge 35d7063)Changes URL declaration to avoid deprecated pattern (Merge 35d7063)refactored retrieval of choices so that the db is getting distinct values; added test (Merge 35d7063)pep8 (Merge 35d7063)Use order_by to avoid ambiguitydrop django-easy-select2 and include select2 directlyTeststest with both Python 3.5 and Django 1.10removed print statement from test (Merge 35d7063)fixed failing test to account for new distinct for max choices (Merge 35d7063)added test to make sure all operators are properly restored from Queries (Merge d3ee9f4)ContributorsPavel SavchenkoPJ PassalacquaHermano Cabral1.0.4 - Unbreak Python 3This release contains a fix to allow distribution installation on Python 3 which was broken since 1.0.21.0.3 - The Package FixThis is a quick fix for packaging (setup.py) errors and documentation.Bugsadd missing Django 1.7 migrationsREADME updated to mentionmanage.py migratecommandUse ReST for README and CHANGELOG: avoid conversion from markdown1.0.2 - A Better FutureThis release features better test coverage and support for Django 1.9.Bugsstretch formset table to the modal container widthtoggle advancedvendor/jquerydir according to Django versionretain support older Django versionsclean up legacy tags in templatesTestsadd admin views testsadd Django 1.9 to test matrixother minor improvementsDocsImprove README with a newer screenshot and pretty tables for badgesContributors:Pavel SavchenkoLeonardo J. Caballero GSchuyler Duveen1.0.1 - A Public ReleaseBugsproper support for py26 and py3X and different Django releasesavoid querying all instances for choicesresolve settings inside view and refine error handlingTestsadd doctests to theform_helpersadd tests forformsadd test caseviews.TestGetFieldChoicesViewsetup.py/travis: addtest-reqs.txtas extras_requirerefactor testing to usepy.testand runtoxfromsetup.pytravis: use latest version of each Django releaseDocs:README: explain what we test against1.0 - First contactMajor changesAdd a new (required) field`AdvancedFilter.model<https://raw.githubusercontent.com/modlinltd/django-advanced-filters/develop/README.rst#model-correlation>`__Add parsing query dict into initialized formsets (allows forediting
existing
instance).Add`AdvancedFilterAdmin<#editing-previously-created-advanced-filters>`__
for actually accessing andediting existing ``AdvancedFilter`instances <https://raw.githubusercontent.com/modlinltd/django-advanced-filters/develop/README.rst#editing-previously-created-advanced-filters>`__.UseSelect2and
an AJAX view to dynamically populate`fieldoptions <https://raw.githubusercontent.com/modlinltd/django-advanced-filters/develop/README.rst#fields>`__.Add proper support for nested serialization of queries.Minor changesImplement more`operators<https://raw.githubusercontent.com/modlinltd/django-advanced-filters/develop/README.rst#operators>`__ (isnull,istrueandisfalse)Allowcustom verbose naming of fields in
advanced_filter_fieldsAdd helper methods to the model to hide (and decouple) core
serialization functionality from users.Strip whitespace in field values validationSetup and packaging (setup.py/MANIFEST.in)HideQSerializercalling logic in the modelAllow modifyingadvanced_filter_formproperty (defaults toAdvancedFilterForm)Correct documentation regarding position of mixin in subclass (issue
#1)