我正在尝试在管理员列表视图中显示外键“公司名称”。但是,列表视图仅显示公司的 (None)。我做错了什么?
管理员.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)
模型.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