django admin list_display 不可调用
django admin list_display not a callable
class branch(models.Model):
name = models.CharField(max_length=10, unique=True)
class company_group(models.Model):
branch = models.ForeignKey(branch, on_delete=CASCADE)
segment = models.ForeignKey(segment, on_delete=CASCADE)
class position_control(models.Model):
company_group = models.ForeignKey(company_group, on_delete=CASCADE)
position = models.ForeignKey(position, on_delete=CASCADE)
rank = models.SmallIntegerField()
valid = models.BooleanField(default=True)
class PositionControlAdmin(admin.ModelAdmin):
list_display = ('company_group__branch', 'position', 'rank', 'valid')
list_filter = ('company_group__branch',)
我收到错误 : (admin.E108) 'list_display[0]' 的值引用了 'company_group__branch',它不是可调用的,是一个属性'PositionControlAdmin',或 'information.position_control'.
上的属性或方法
与 list_filter company_group__branch 一起工作正常。但在 list_display 中出现错误。我该如何解决?
对于列表显示,您使用的是一个函数,而不是一串下划线分隔的字段名称,因此:
class PositionControlAdmin(admin.ModelAdmin):
list_display = (<strong>'branch_name'</strong>, 'position', 'rank', 'valid')
list_filter = ('company_group__branch',)
@admin.display(description='Branch name')
def <strong>branch_name</strong>(self, object):
return object.company_group.branch.name
class branch(models.Model):
name = models.CharField(max_length=10, unique=True)
class company_group(models.Model):
branch = models.ForeignKey(branch, on_delete=CASCADE)
segment = models.ForeignKey(segment, on_delete=CASCADE)
class position_control(models.Model):
company_group = models.ForeignKey(company_group, on_delete=CASCADE)
position = models.ForeignKey(position, on_delete=CASCADE)
rank = models.SmallIntegerField()
valid = models.BooleanField(default=True)
class PositionControlAdmin(admin.ModelAdmin):
list_display = ('company_group__branch', 'position', 'rank', 'valid')
list_filter = ('company_group__branch',)
我收到错误
与 list_filter company_group__branch 一起工作正常。但在 list_display 中出现错误。我该如何解决?
对于列表显示,您使用的是一个函数,而不是一串下划线分隔的字段名称,因此:
class PositionControlAdmin(admin.ModelAdmin):
list_display = (<strong>'branch_name'</strong>, 'position', 'rank', 'valid')
list_filter = ('company_group__branch',)
@admin.display(description='Branch name')
def <strong>branch_name</strong>(self, object):
return object.company_group.branch.name