Filter a list in the Django admin based on a calculated field
The Django admin list_filter option only accepts model field names. You cannot filter based on a model method or callable, although you can display the value of a model method in the list_display option. If you want to filter by a calculated value, you will need to use a custom filter that queries the database for the objects you want.
You may be better off creating a non-editable database field, overriding the save()
method to populate that field, and then using that field in the default list_filter.
Create a custom Django admin list filter that implements the logic of your calculated field
- Create your model
- Define a custom list filter that queries the database according to whatever logic you need
- Add your custom filter to the list_filter option of your admin class