Home > Net >  django admin list_display not a callable
django admin list_display not a callable

Time:03-14

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',)

I got error <class 'information.admin.PositionControlAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'company_group__branch', which is not a callable, an attribute of 'PositionControlAdmin', or an attribute or method on 'information.position_control'.

With list_filter company_group__branch is working fine. But got error in list_display. How can I fix that?

CodePudding user response:

For the list display, you work with a function, not with a chain of underscore separated field names, so:

class PositionControlAdmin(admin.ModelAdmin):
    list_display = ('branch_name', 'position', 'rank', 'valid')
    list_filter = ('company_group__branch',)

    @admin.display(description='Branch name')
    def branch_name(self, object):
        return object.company_group.branch.name
  • Related