更改管理员字段过滤器名称django 1.7 - ModelAdmin.list_filter

时间:2016-11-18 00:14:52

标签: python django

基本上,我正在尝试为我的过滤器字段创建自定义名称,因为原始数据库值并不具有多大意义。当前值为0,1和?。我在这里的模板管理器列表https://docs.djangoproject.com/en/1.10/ref/contrib/admin/上关注了Django管理员指南。

以下是我的代码:

library(dplyr)
inner_join(person.data, daily.data) %>%
  group_by(date, week_code) %>%
  summarize(absence_count = sum(present_absent == 'a'))

我的模型(数据库)中的几行:

@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 = '?')

我的数据库模型:

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

我尝试运行Django时遇到的错误是:

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"

我之后更改下面红框中的值:

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为你的第三行应该是这样的:

// Empty list
cell empty = Nil();

// [2, 3]
cell sublist23 = Pair(Int(2),
                    Pair(Int(3), empty));

cell sublist11 = Pair(Int(1), Pair(Int(1), empty));
cell sublist1_11 = Pair(Int(1), sublist11);

// [1, [2, 3], 4]
cell list1 = Pair(Int(1), 
                  Pair(sublist23, 
                       Pair(Int(4), empty)));
cell list2 = Pair(sublist1_11, sublist1_11);

没有引号。