基本上,我尝试为我的过滤器字段创建自定义名称,因为原始数据库值没有太多含义。当前值为0、1和?。我在这里遵循了Django管理指南https://docs.djangoproject.com/en/1.10/ref/contrib/admin/部分ModelAdmin.list_过滤器. 在
以下是我的代码:
@admin.register(AuditPolicies)
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
`self.value()`.
"""
# 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 = '?')
我的模型(数据库)中的几行:
^{pr2}$我的数据库模型:
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"
我尝试运行Django时遇到的错误是:
ERRORS:
<class 'policy_manager.apps.policy.admin.AuditPoliciesAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'PolicyComparisonFilter', which does not refer to a Field.
我要做的是更改下面红色框中的值:
我觉得你的第三行应该是这样的:
没有引号。在
相关问题 更多 >
编程相关推荐