Django Admin:带有def函数变量的list_filter和date_hierarchy
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django Admin:带有def函数变量的list_filter和date_hierarchy相关的知识,希望对你有一定的参考价值。
这是我的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.朋友
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.朋友
class ReportSummaryAdmin(admin.ModelAdmin):
list_display = ['reportbranch','reportdate','particular','Report','total_amount','quantity']
list_filter = ['particular','reportbranch']
date_hierarchy = 'reportdate'
为什么抛出这样的问题,我需要用init函数定义任何东西吗?我对这个问题的原因及其修复感到困惑。
答案
哎呀!!这是一个我忽略的简单修复。
我用这段代码修好了
list_filter = ['Report__branch']
date_hierarchy = 'Report__date'
以上是关于Django Admin:带有def函数变量的list_filter和date_hierarchy的主要内容,如果未能解决你的问题,请参考以下文章
Django-admin.py startproject --带有模板标签的模板
带有空白表的 Django 2.1 admin.py changeform_view 方法错误