carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections
https://django-filter.readthedocs.io/en/main/
Other
4.47k stars 770 forks source link

django-filters prevents django admin app from starting #1694

Closed rmunoz10 closed 6 days ago

rmunoz10 commented 6 days ago

Hello,

Trying to add django-filters to my django admin app. App works without filter. But runserver errors out with "The value of 'list_filter[0]' must inherit from 'ListFilter'." when I add "list_filter = (SfAccountFilter,)" to admin.py. See below for details

Not sure what I'm missing

I would appreciate any insight

Thanks


admin.py

from django.contrib import admin

Register your models here.

from .models import SfAccountHierarchy from .filters import SfAccountFilter

Define the admin class

class SfAccountHierarchyAdmin(admin.ModelAdmin): list_filter = (SfAccountFilter,)

Register the admin class with the associated model

admin.site.register(SfAccountHierarchy, SfAccountHierarchyAdmin)


filters.py

import django_filters from .models import SfAccountHierarchy

class SfAccountFilter(django_filters.FilterSet): account_nm = django_filters.CharFilter(lookup_expr='icontains', label='Account Name')
class Meta: model = SfAccountHierarchy fields = ['account_nm']


models.py

from django.db import models

Create your models here.

from django.urls import reverse # Used in get_absolute_url() to get URL for specified ID

class SfAccountHierarchy(models.Model): id = models.CharField(primary_key=True, max_length=30, help_text='Account ID') account_nm = models.CharField(max_length=60) master_parentid = models.CharField(blank=True, null=True, max_length=30) master_account_nm = models.CharField(blank=True, null=True, max_length=60) parentid = models.CharField(blank=True, null=True, max_length=30) parent_account_nm = models.CharField(blank=True, null=True, max_length=30) account_nm_alias = models.CharField(blank=True, null=True, max_length=60) parent_account_nm_alias = models.CharField(blank=True, null=True, max_length=60)

class Meta:
    managed = False
    db_table = 'sf_account_hierarchy'

def __str__(self):
    return self.id

def get_absolute_url(self):
    """Returns the URL to access a detail record for this account."""
    return reverse('account-detail', args=[str(self.id)])

settings.py

...

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_filters', # Add django-filter 'catalog', # Add your app
]


Exception in thread django-main-thread: Traceback (most recent call last): File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner self.run() File "/usr/lib/python3.10/threading.py", line 953, in run self._target(*self._args, *self._kwargs) File "/home/rmunoz/dev/my_django_project/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper fn(args, **kwargs) File "/home/rmunoz/dev/my_django_project/venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run self.check(display_num_errors=True) File "/home/rmunoz/dev/my_django_project/venv/lib/python3.10/site-packages/django/core/management/base.py", line 563, in check raise SystemCheckError(msg) django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS: <class 'catalog.admin.SfAccountHierarchyAdmin'>: (admin.E113) The value of 'list_filter[0]' must inherit from 'ListFilter'.

System check identified 1 issue (0 silenced).

carltongibson commented 6 days ago

Django Filter's filters are not the same thing as admin filters. You need to use those:

https://docs.djangoproject.com/en/5.1/ref/contrib/admin/filters/