Login

Snippets by ssokolow

Snippet List

Closure for FieldListFilter classes with custom sets of ranges

This closure lets you quickly produce date-style range filters in the Django Admin interface without having to create a new class for each one. It follows Python range semantics, with the lower value using a `_gte` test and the upper value using an `_lt` test. Here's an example of how I'm using it in one of my projects: list_filter = ('complete', ('chapters', makeRangeFieldListFilter([ (_('1'), 1, 2), (_('2 to 10'), 2, 10), (_('11 to 30'), 11, 30), (_('31 to 100'), 31, 100), (_('At least 100'), 100, None), ], nullable=True)), ('word_count', makeRangeFieldListFilter([ (_('Less than 1000'), None, 1000), (_('1K to 5K'), 1000, 5000), (_('5K to 10K'), 5000, 10000), (_('10K to 75K'), 10000, 75000), (_('75K to 150K'), 75000, 150000), (_('150K to 300K'), 150000, 300000), (_('At least 300K'), 300000, None), ], nullable=True)), ('derivatives_count', makeRangeFieldListFilter([ (_('None'), 0, 1), (_('1 to 5'), 1, 5), (_('5 to 50'), 5, 50), (_('50 to 1000'), 50, 1000), (_('At least 1000'), 1000, None), ])), 'pub_date', 'upd_date') It is based on code from `DateFieldListFilter` and `BooleanFieldListFilter` from `django.contrib.admin.filters`.

  • filter
  • django
  • admin
  • fieldlistfilter
Read More

IsNullFieldListFilter

As the title does a pretty good job of condensing, this is a subclass of `FieldListFilter` for the Django 1.4 Admin system which allows you filter by whether a field is or is not `NULL`. For example, if you had an `Author` model and wanted to filter it by whether authors were also users of the site, you could add this to your `AuthorAdmin` class: list_filter = ( ('user_acct', IsNullFieldListFilter), ) For the record, it began life as a modified version of `BooleanFieldListFilter` from `django.contrib.admin.filters`.

  • filter
  • django
  • admin
  • fieldlistfilter
Read More

ssokolow has posted 2 snippets.