Python Django:列表显示管理中的外键值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14130174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:33:03  来源:igfitidea点击:

Django: foreign key value in a list display admin

pythondjangodjango-admin

提问by Prometheus

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?

我正在尝试在管理列表视图中显示外键“公司名称”。但是,列表视图只显示公司的 (None)。我做错了什么?

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

模型.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

采纳答案by Blair

Your Campaignmodel has no Companyattribute - the ForeignKey is the field companyid. You'd need to change your function to

您的Campaign模型没有Company属性 - ForeignKey 是 field companyid。你需要改变你的功能

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']

回答by Jim K.

An additional feature to consider when you're linking to a ForeignKey object in this way is to set related_company.allow_tags = Trueon 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 并将其呈现为列表视图中的可用链接。