Justin S Justin S - 23 days ago 6
Python Question

Change admin field filter name django 1.7 - ModelAdmin.list_filter

Basically, I am trying to create custom names for my filter fields as the original database values don't hold much meaning. The current values are 0,1 and ?. I followed the Django admin guide here https://docs.djangoproject.com/en/1.10/ref/contrib/admin/ on section ModelAdmin.list_filter.

Below is my code:

class AuditPoliciesAdmin(admin.ModelAdmin):
list_filter = (PolicyComparisonFilter,)

class PolicyComparisonFilter(admin.SimpleListFilter):
title = _('Policy Comparison')
parameter_name = 'SourceState'

def lookups(self, request, model_admin):
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
return (
('0', _('No Match')),
('1', _('Match')),
('?', _('Missing')),

def queryset(self, request, queryset):
Returns the filtered queryset based on the value
provided in the query string and retrievable via
# Compare the requested value (either '80s' or '90s')
# to decide how to filter the queryset.
if self.value() == '0':
return queryset.filter(SourceState__gte = '0')
if self.value() == '1':
return queryset.filter(SourceState__gte = '1')
if self.value() == '?':
return queryset.filter(SourceState__gte = '?')

And a couple of lines from my model(database):

364 STASH master-bi-prod-s3-ilayer-sen-group-user /master-bi-prod-s3-ilayer-sen-group-user.json 1 MASTER master-bi-prod-s3-ilayer-sen-group-user arn:aws:iam::588738232867:policy/master-bi-prod-s3-ilayer-sen-group-user 1 2016-11-17 04:04:16.117000
365 STASH master-bi-prod-s3-pop-admin /master-bi-prod-s3-pop-admin.json 0 MASTER master-bi-prod-s3-pop-admin arn:aws:iam::588738232867:policy/master-bi-prod-s3-pop-admin 0 2016-11-17 04:04:16.117000

My database model:

class AuditPolicies(models.Model):
ComparisonDate = models.DateTimeField(default=datetime.now(), blank=True)
Source = models.CharField(max_length=32, blank=True, null=True)
SourcePolicyName = models.CharField(max_length=64, blank=True, null=True)
SourcePolicyPath = models.CharField(max_length=128, blank=True, null=True) # todo: check if this should be using models.SlugField()
SourceState = models.CharField(max_length=2, blank=True, null=True)

Target = models.CharField(max_length=32, blank=True, null=True)
TargetPolicyName = models.CharField(max_length=64, blank=True, null=True)
TargetPolicyPath = models.CharField(max_length=128, blank=True, null=True)
TargetState = models.CharField(max_length=2, blank=True, null=True)

class Meta(object):
verbose_name_plural = "Audit Policies"

The error I get when I try to run django is:

Traceback (most recent call last):
File "D:/dev/projects/policy-manager/manage.py", line 10, in <module>
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\core\management\__init__.py", line 354, in execute
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\__init__.py", line 21, in setup
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\apps\registry.py", line 115, in populate
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\contrib\admin\apps.py", line 22, in ready
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\contrib\admin\__init__.py", line 23, in autodiscover
autodiscover_modules('admin', register_to=site)
File "D:\dev\virtual_envs\policy_manager\lib\site-packages\django\utils\module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "D:\apps\Python\Python27\Lib\importlib\__init__.py", line 37, in import_module
File "D:\dev\projects\policy-manager\policy_manager\apps\policy\admin.py", line 175, in <module>
class AuditPoliciesAdmin(admin.ModelAdmin):
File "D:\dev\projects\policy-manager\policy_manager\apps\policy\admin.py", line 188, in AuditPoliciesAdmin
list_filter = (PolicyComparisonFilter,)
NameError: name 'PolicyComparisonFilter' is not defined

What I'm after is changing thee values in the red box below:

enter image description here


I think your third line should look like this:

list_filter = (PolicyComparisonFilter, )

Without the quotes.