# django admin get\_list\_filter

**URL:** <https://forum.djangoproject.com/t/django-admin-get-list-filter/35221>\
**Category:** The Admin\
**Created:** [September 29, 2024, 3:25pm UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221 "2024-09-29T15:25:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ffarzam](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ffarzam/32/20078_2.png) [@ffarzam](https://forum.djangoproject.com/u/ffarzam)\
**Post date:** [September 29, 2024, 3:25pm UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221/1 "2024-09-29T15:25:50Z")

</div>

this is my get\_list\_filter method in user admin

```auto
    def get_list_filter(self, request):
        list_filter = self.list_filter

        if request.user.is_superuser:
            list_filter.append('is_superuser')

        return list_filter

```

the problem is after each time i call user admin panel in browser, an new is\_superuser field wil be add to list filter.

just like this:  
first time:` list_filter = [<class 'octopus.accounts.admin.user.UserInfoSearch'>, <class 'octopus.accounts.admin.user.UserEmailSearch'>, 'is_staff', 'groups', <class 'octopus.accounts.admin.user.UserRoleStatusFilter'>, <class 'octopus.accounts.admin.reward.UserBuildingFilter'>, <class 'octopus.accounts.admin.user.WorkmanStateFilter'>, 'is_superuser']`

second time: `list_filte = [<class 'octopus.accounts.admin.user.UserInfoSearch'>, <class 'octopus.accounts.admin.user.UserEmailSearch'>, 'is_staff', 'groups', <class 'octopus.accounts.admin.user.UserRoleStatusFilter'>, <class 'octopus.accounts.admin.reward.UserBuildingFilter'>, <class 'octopus.accounts.admin.user.WorkmanStateFilter'>, 'is_superuser', 'is_superuser']`

third time: `list_filte = [<class 'octopus.accounts.admin.user.UserInfoSearch'>, <class 'octopus.accounts.admin.user.UserEmailSearch'>, 'is_staff', 'groups', <class 'octopus.accounts.admin.user.UserRoleStatusFilter'>, <class 'octopus.accounts.admin.reward.UserBuildingFilter'>, <class 'octopus.accounts.admin.user.WorkmanStateFilter'>, 'is_superuser', 'is_superuser', 'is_superuser']`

as you see is\_superuser is added each time!

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [September 29, 2024, 4:28pm UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221/2 "2024-09-29T16:28:55Z")

</div>

Try this:

```auto
def get_list_filter(self, request):
    list_filter = list(self.list_filter) # Create a new list from self.list_filter

    if request.user.is_superuser and 'is_superuser' not in list_filter:
        list_filter.append('is_superuser')

    return list_filter

```

---

<div class="post-metadata">

**Author:** ![ffarzam](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ffarzam/32/20078_2.png) [@ffarzam](https://forum.djangoproject.com/u/ffarzam)\
**Post date:** [September 29, 2024, 4:39pm UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221/3 "2024-09-29T16:39:31Z")

</div>

thanks for the solution, but i want to know why this happened!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 29, 2024, 6:06pm UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221/4 "2024-09-29T18:06:19Z")

</div>

> [@ffarzam](#):
>
> ```auto
> list_filter = self.list_filter
> ^^^^^^^^^^^^^^^^
> 
> ```

This is the mistake.

You’re getting a reference to the actual class attribute, and so you’re mutating that attribute in the class, not a local copy of it. (That attribute is a class attribute, not an instance attribute.)

I expect that after doing this, if you were to log out and log back in as a non-superuser, you would still see this filter being shown.

You want to make a copy (new instance) of that attribute to modify it for that instance and return that copy.

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [September 30, 2024, 12:59am UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221/5 "2024-09-30T00:59:37Z")

</div>

The result you want can be expressed as follows:

a = 0~9  
if a is greater than 5, add 1 to a

```auto
a = {some function}
if 5 < a:
  a += 1

```

same as

```auto
def get_list_filter(self, request):
  list_filter = self.list_filter
  if 'is_superuser' not in list_filter and request.user.is_superuser:
    list_filter.append('is_superuser')
  return list_filter

```

But, your code just add 1 to the size of a

```auto
a = {some function}
a += 1

```

same as

```auto
def get_list_filter(self, request):
  list_filter = self.list_filter
  if request.user.is_superuser:
    list_filter.append('is_superuser')
  return list_filter

```

---

<div class="post-metadata">

**Author:** ![ffarzam](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ffarzam/32/20078_2.png) [@ffarzam](https://forum.djangoproject.com/u/ffarzam)\
**Post date:** [September 30, 2024, 4:35am UTC](https://forum.djangoproject.com/t/django-admin-get-list-filter/35221/6 "2024-09-30T04:35:25Z")

</div>

yes you are right about this `I expect that after doing this, if you were to log out and log back in as a non-superuser, you would still see this filter being shown.`

thanks for the explanation
