Developing change list filters

If you want the administrators to be able to filter the change list by date, relation, or field choices, you need to use the list_filter property for the admin model. Additionally, there is a possibility of having custom-tailored filters. In this recipe, we will add a filter that allows you to select products by the number of photos attached to them.

Getting ready

Let's start with the products app that we created in the previous recipe.

How to do it...

Execute the following two steps:

  1. In the admin.py file, create a PhotoFilter class extending from SimpleListFilter, as follows:
    # products/admin.py # -*- coding: UTF-8 -*- # ... all previous imports go here ... from django.db import models class PhotoFilter(admin.SimpleListFilter): ...

Get Django: Web Development with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.