简体   繁体   English

Django:列表中的外键值显示admin

[英]Django: foreign key value in a list display admin

I'm trying to display the foreign key 'company name' in the admin list view. 我正在尝试在管理列表视图中显示外键“公司名称”。 However, the list view just shows (None) for the company. 但是,列表视图只显示公司的(无)。 What I'm I doing wrong? 我做错了什么?

admin.py admin.py

class CampaignAdmin(admin.ModelAdmin):
    #fields = ['name', 'Company_name', 'active', 'modified', 'created']
    list_display = ['name', 'related_company', 'active', 'modified', 'created']
    list_filter = ['active']
    search_fields = ['name']
    sortable_field_name = "name"
    autocomplete_lookup_fields = {
        'name': ['name'],
        }

    def related_company(self, obj):
        return '%s'%(obj.Company.name)
    related_company.short_description = 'Company'


admin.site.register(Campaign, CampaignAdmin)

model.py model.py

class Company(models.Model):
    companyid = models.CharField(max_length=255, primary_key=True, db_column='companyID')
    name = models.CharField(max_length=105)
    logourl = models.CharField(max_length=255, db_column='logoURL')
    website = models.CharField(max_length=255, blank=True)
    active = HibernateBooleanField(default=False)
    created = models.DateTimeField()
    modified = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = u'company'
        ordering = ['name']

    @staticmethod
    def autocomplete_search_fields():
        return ("id__iexact", "name__icontains",)

    def __unicode__(self):
        return self.name


class Campaign(models.Model):
    campaignid = models.CharField(max_length=255, primary_key=True, db_column='campaignID')
    name = models.CharField(max_length=105)
    active = HibernateBooleanField(default=False)
    created = models.DateTimeField()
    modified = models.DateTimeField(null=True, blank=True)
    companyid = models.ForeignKey(Company, null=True, db_column='companyID', blank=True)

    class Meta:
        db_table = u'campaign'


    def __unicode__(self):
        return self.name

Your Campaign model has no Company attribute - the ForeignKey is the field companyid . 您的Campaign模型没有Company属性 - ForeignKey是字段companyid You'd need to change your function to 您需要将功能更改为

def related_company(self, obj):
    return obj.companyid.name
related_company.short_description = 'Company'

And since the __unicode__() method of the company object returns the name anyway, you probably don't need the custom function anyway - I think you can put the foreign key field directly in the display list: 并且由于公司对象的__unicode__()方法仍然返回名称,您可能无论如何都不需要自定义函数 - 我认为您可以将外键字段直接放在显示列表中:

list_display = ['name', 'companyid', 'active', 'modified', 'created']

An additional feature to consider when you're linking to a ForeignKey object in this way is to set related_company.allow_tags = True on the function. 以这种方式链接到ForeignKey对象时要考虑的另一个特性是在函数上设置related_company.allow_tags = True

This will make it so you can return HTML and have it be rendered as a usable link in the listview. 这将使您可以返回HTML并将其呈现为列表视图中的可用链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM