Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A Django application for advanced admin filters

Project Description

Django Advanced Filters

Branch Build Coverage PyPI Gitter
Master
Develop    

A django ModelAdmin mixin which adds advanced filtering abilities to the admin.

Mimics the advanced search feature in VTiger, see here for more info

For release notes, see Changelog

Requirements

  • Django >= 1.5 (Django 1.5 - 1.9 on Python 2/3/PyPy2)
  • django-braces == 1.4.0
  • simplejson == 3.6.5

NOTE: While the latest Django 1.5.X is supported, the bundled jQuery it includes is outdated (1.4.2) and as such, most of our admin frontend scripts fail. This means that to use advanced-filters in Django 1.5 admin, you’d have to probably include your own jQuery (1.9 or later) and add it to global namespace prior to other scripts in AdvancedFilterForm.Meta.

Installation & Set up

  1. Install from pypi: pip install django-advanced-filters
  2. Add both 'advanced_filters' to INSTALLED_APPS.
  3. Add url(r'^advanced_filters/', include('advanced_filters.urls')) to your project’s urlconf.
  4. Run python manage.py syncdb or python manage.py migrate (for django >= 1.7)

Integration Example

Extending a ModelAdmin is pretty straightforward:

from advanced_filters.admin import AdminAdvancedFiltersMixin

class ProfileAdmin(AdminAdvancedFiltersMixin, models.ModelAdmin):
    list_filter = ('name', 'language', 'ts')   # simple list filters

    # select from these fields in the advanced filter creation form
    advanced_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 fields

Initially, each field in advanced_filter_fields is 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:

class ProfileAdmin(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:

class ProfileAdmin(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 filters

By default the mixin uses a template which extends django’s built-in change_list template. 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 superb magnificPopup which 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.

Structure

Each 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, value

And allows the optional negate and remove fields.

Let us go over each of the fields in a rule fieldset.

Field

The list of all available fields for this specific instance of the ModelAdmin as specific by the `advanced_filter_fields property. <#integration-example>`__

The OR field

OR is an additional field that is added to every rule’s available fields.

It allows constructing queries with OR statements. You can use it by creating an “empty” rule with this field “between” a set of 1 or more rules.

Operator

Query field suffixes which specify how the WHERE query will be constructed.

The currently supported are as follows: iexact, icontains, iregex, range, isnull, istrue and isfalse

For more detail on what they mean and how they function, see django’s documentation on field lookups.

Value

The 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)

Negate

A boolean (check-box) field to specify whether this rule is to be negated, effectively making it a “exclude” sub-query.

Remove

Similarly to other django formsets, used to remove the formset on submit.

Editing previously created advanced filters

The AdvancedFilterAdmin class (a subclass of ModelAdmin) is provided and registered with AdvancedFilter in 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 Serialization

TODO: write a few words on how serialization of queries is done.

Model correlation

Since version 1.0, The underlying AdvancedFilter model instances are tightly coupled with a specific model (using the app_label.Name model name), for which admin changelist they are to used and created in.

This change has a few benefits:

  1. Admin mixin can be used with multiple ModelAdmin classes while performing specific query serialization and field validation that are at the base of the filter functionality.
  2. Allows users to edit previously created filters outside of the context of a changelist, as we do in the `AdvancedFilterAdmin <#editing-previously-created-advanced-filters>`__.
  3. Limit the AdvancedListFilters to limit queryset (and thus, the underlying options) to a specified model.

Note: Since we are at the early stages of development I have skipped the South / 1.7 schema (new field) and data migrations (add specific model to all existing instances of AdvancedFilter model) migrations. Though this shouldn’t be too difficult to do, if the need arises I can add migration examples.

Views

The GetFieldChoices view is required to dynamically (using javascript) fetch a list of valid field choices when creating/changing an AdvancedFilter.

TODO

  • Add permission user/group selection functionality to the filter form
  • Allow toggling of predefined templates (grappelli / vanilla django admin), and front-end features.
  • Support more (newer) python/django versions

Changelog

1.0.6 - Bout Time

This release is long overdue, and includes some important fixes as well as general improvements to code and documentation.

Bug Fixes

  • fixing TypeError: can only concatenate tuple (not “list”) to tuple
  • ensure select2 is included last (Merge 9831ba5)
  • add script to load jQuery globally
  • remove invalid template variables
  • fix input focusing error in chrome
  • fix error when one missing range parameter caused error + test (Merge 365b646)

Features

  • don’t override original change_list_templates in AdminAdvancedFiltersMixin
  • make date range placeholder more pleasant (Merge 365b646)
  • add created_at field
  • Russian locale provided

Documentation

  • make it clear easy-select2 is not required anymore (Merge 9831ba5)
  • Clarify how to import AdminAdvancedFiltersMixin in README

Tests

  • add more fields/filter to test ModelAdmin

Contributors

  • Grigoriy Beziuk
  • Никита Конин
  • Pavel Savchenko
  • Yuval Adam
  • Petr Dlouhý

1.0.5 - Compatibility bump

Bugs

  • updated 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 pattern
  • select2 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 ambiguity
  • drop django-easy-select2 and include select2 directly

Tests

  • test with both Python 3.5 and Django 1.10
  • removed 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)

Contributors

  • Pavel Savchenko
  • PJ Passalacqua
  • Hermano Cabral

1.0.4 - Unbreak Python 3

This release contains a fix to allow distribution installation on Python 3 which was broken since 1.0.2

1.0.3 - The Package Fix

This is a quick fix for packaging (setup.py) errors and documentation.

Bugs

  • add missing Django 1.7 migrations
  • README updated to mention manage.py migrate command
  • Use ReST for README and CHANGELOG: avoid conversion from markdown

1.0.2 - A Better Future

This release features better test coverage and support for Django 1.9.

Bugs

  • stretch formset table to the modal container width
  • toggle advanced vendor/jquery dir according to Django version
  • retain support older Django versions
  • clean up legacy tags in templates

Tests

  • add admin views tests
  • add Django 1.9 to test matrix
  • other minor improvements

Docs

  • Improve README with a newer screenshot and pretty tables for badges

Contributors:

  • Pavel Savchenko
  • Leonardo J. Caballero G
  • Schuyler Duveen

1.0.1 - A Public Release

Bugs

  • proper support for py26 and py3X and different Django releases
  • avoid querying all instances for choices
  • resolve settings inside view and refine error handling

Tests

  • add doctests to the form_helpers
  • add tests for forms
  • add test case views.TestGetFieldChoicesView
  • setup.py/travis: add test-reqs.txt as extras_require
  • refactor testing to use py.test and run tox from setup.py
  • travis: use latest version of each Django release

Docs:

  • README: explain what we test against

1.0 - First contact

Major changes

Minor changes

Release History

Release History

This version
History Node

1.0.6

History Node

1.0.5

History Node

1.0.4

History Node

1.0.3

History Node

1.0.2

History Node

1.0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django_advanced_filters-1.0.6-py2.py3-none-any.whl (74.7 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Sep 3, 2017
django-advanced-filters-1.0.6.tar.gz (61.7 kB) Copy SHA256 Checksum SHA256 Source Sep 3, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting