Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin: use checkboxes in list view in list_filter()

I have a model Transaction with a ForeignKey to another model (TransactionState) on state field. So in admin.py I have:

class TransactionAdmin(admin.ModelAdmin):
    ...
    list_filter = ('state', )
    ...

In TransactionState I have records like "paid", "unpaid", "delivered", "canceled", Etc. and it works fine but I want to be able to filter using checkboxes to allow multiple selection like "paid" OR "delivered". It's possible?

like image 691
edepe Avatar asked Feb 12 '15 21:02

edepe


People also ask

How do you check checkbox is checked or not in Django?

POST["something_truthy"] is True if that checkbox was checked, and False if not.

What is List_filter in Django?

list_filter to a list or tuple of elements, where each element is one of the following types: A field name. A subclass of django. contrib. admin.

What we can do in admin portal in Django?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


1 Answers

For all models

You can easily override the django admin templates to customize the admin UI.

To edit the sidebar filter, just add a templates/admin/filter.html file, and write your custom HTML with radio buttons.

Note that this will change the sidebar filter for all models.

For a single model

If you want to change the filter for a single model, you can specify a template for a ListFilter:

class FilterWithCustomTemplate(admin.SimpleListFilter):
    template = "custom_template.html"

Example

As a reference example, check is the default template for filter.html.

like image 165
David Arcos Avatar answered Oct 02 '22 14:10

David Arcos