Django Admin:具有def函数变量的list_filter和date_hierarchy

时间:2019-04-14 09:18:58

标签: python django django-models django-admin

这是我的models.py和admin.py文件,应该在其中使用“ reportbranch”和“ date_hierarchy”与“ reportdate”进行过滤。但它引发以下错误:

ERRORS:
 <class 'reports.admin.ReportSummaryAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'reportbranch', which does not refer to a Field.
 <class 'reports.admin.ReportSummaryAdmin'>: (admin.E127) The value of 'date_hierarchy' refers to 'reportdate', which does not refer to a Field.

models.py

    class DailyReport(models.Model):
        report_type = models.CharField(max_length=500, choices=REPORT_TYPE, verbose_name="Type")
        date = models.DateField(verbose_name="Date")
        branch = models.ForeignKey(Branch, on_delete=models.CASCADE, verbose_name="Branch")
        added_by = models.ForeignKey(User, editable=False, on_delete=models.CASCADE, null=True, blank=True)
        added_at = models.DateTimeField(auto_now=True, editable=False)

        def __str__(self):
            return str(self.date) + ' - ' +self.branch.name


    class ReportItem(models.Model):
        particular = models.ForeignKey(Particular, on_delete=models.CASCADE, verbose_name="Particular")
        Report = models.ForeignKey(DailyReport, on_delete=models.CASCADE)
        total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="Total Amount")
        quantity = models.IntegerField(verbose_name="Quantity")
        remarks = models.CharField(max_length=5000, verbose_name="Remarks", null=True, blank=True)


    class ReportSummary(ReportItem):
        class Meta:
            proxy = True
            verbose_name = "Daily Report Summary"
            verbose_name_plural = "Daily Report Summaries"

        def reportdate(self):
            return self.Report.date

        def reportbranch(self):
            return self.Report.branch.name

admin.py

class ReportSummaryAdmin(admin.ModelAdmin):
list_display = ['reportbranch','reportdate','particular','Report','total_amount','quantity']
list_filter = ['particular','reportbranch']
date_hierarchy = 'reportdate'

为什么会引发此类问题,我是否需要使用 init 函数定义任何内容?我对该问题的原因及其解决方法感到困惑。

1 个答案:

答案 0 :(得分:0)

糟糕!这是我忽略的简单修复。

我用此代码修复了该问题

list_filter = ['Report__branch']
date_hierarchy = 'Report__date'