K
K
kondratev-ad2022-04-12 17:18:51
Django
kondratev-ad, 2022-04-12 17:18:51

How to make a global filter in Django admin?

Is it possible to make a global filter in the admin panel?
For example, there are models:

class Company(models.Model):
    name = models.CharField(
        'Название компании',
        max_length=200,
        help_text='Название компании'
    )
    city = models.CharField(
        'Город',
        max_length=200,
        help_text='Город'
    )

class Object(models.Model):
    name = models.CharField(
        'Название объекта',
        max_length=200,
        help_text='Название объекта'
    )
    number = models.CharField(
        'Номер объекта',
        max_length=200,
        help_text='Номер объекта'
    )
    company = models.ForeignKey(
        Company,
        on_delete=models.SET_NULL,
        verbose_name='Компания',
        null=True
    )

class Person(models.Model):
    name = models.CharField(
        'Имя,
        max_length=200,
        help_text='Имя'
    )
    number = models.CharField(
        'ID персоны',
        max_length=200,
        help_text='ID персоны'
    )
    object = models.ForeignKey(
        Object,
        on_delete=models.SET_NULL,
        verbose_name='Объект',
        null=True
    )

It turns out that there are several companies. Each company has several facilities, which in turn have staff. I would like to make a global filter by Companies in the admin panel. That is, not every time to filter, but let's say I chose the company "Horns and Hooves" and calmly worked in the admin panel, with objects belonging to this company.
This is just an example, in reality there are many more models

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-04-12
@bacon

That’s not right, but here’s a crutch option, make a model that is tied to the user and store filtering settings in it, write a mixin that overrides get_queryset according to these settings, add this mixin to the necessary ModelAdmin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question