Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin List Filter Remove All Option

As you can see from the screenshot bellow, When using and customizing django admin filters like so:

class DealExpiredListFilter(admin.SimpleListFilter):
  title = 'expired'

  parameter_name = 'expired'

  def lookups(self, request, model_admin):
      return (
          ('yes', 'yes'),
          ('no', 'no'),
      )

  def queryset(self, request, queryset):
      if self.value() == "yes":
          return queryset.filter(end_date__lt=timezone.now())
      elif self.value() == "no":
          return queryset.exclude(end_date__lt=timezone.now())

Django will insert an "All" option at any case (which is great for %99 of the times). I want to rename or remove that "All" option as can bee seen in the screenshot

scrrenshot

like image 674
elad silver Avatar asked Dec 17 '18 19:12

elad silver


People also ask

How to filter the instances of a model in Django?

Django allows the user of the admin site to filter the instances of a Model by adding the list_filter attribute to your ModelAdmin objects. You can find more information about the Django’s filtering utilities in the official documentation, in the Django Admin Site section.

How to add a Boolean to list_filter in Django?

just adding name of the field to list_filter will add a filter of that field on the admin side automatically. This is how Django treats the default field types. The boolean appear as All, Yes and No. which upon selection apply that filter and show the results. similarly the choice field we added in the model.

How to add autocomplete list filter in Django?

So to solve this, there is a simple app which uses Django’s autocomplete widget to render the list filter. Then add admin_auto_filters to your INSTALLED_APPS inside settings.py of your project. Let’s say we have the following models:

How to add datetime field to list_filter in Django?

simply adding your DateTime field to list_filter will add the filter to listing. but there are limitions added by django on UI of default date filter which looks something like: ... list_filter = ("created_at", "role") ... The DateTime field, upon adding to list_filter shows these 5 options Any date, Today, Past 7 days, This month, This year.


1 Answers

I eventually solved it by browsing the source code which can be found [here in this link][1] and overriding the choices function. I only changed the "All" selection to show "Yes" in this code that was added to my filter class :

from django.utils.encoding import force_text

def choices(self, changelist):
    """Copied from source code to remove the "All" Option"""
    yield {
        'selected': self.value() is None,
        'query_string': changelist.get_query_string({}, [self.parameter_name]),
        'display': 'Yes',
    }
    for lookup, title in self.lookup_choices:
        yield {
            'selected': self.value() == force_text(lookup),
            'query_string': changelist.get_query_string({self.parameter_name: lookup}, []),
            'display': title,
        }

As Linh mentioned: For anyone only want to remove the all option just delete 'display': 'Yes', from the first yield{} in choices function [1]: https://github.com/django/django/blob/main/django/contrib/admin/filters.py#L44

like image 151
elad silver Avatar answered Oct 19 '22 10:10

elad silver