Django管理列表显示+ ForeignKey =空变更列表 [英] Django admin List Display + ForeignKey = Empty Change List
问题描述
我在django管理员有一个奇怪的问题 list_display
。每当我将一个外键添加到 list_display
中时,整个更改列表视图将只显示条目总数。
models.py:
class Organization(models.Model):
org_id = models.AutoField(primary_key = True)
org_name = models.CharField(max_length = 288)
def __unicode __(self):
return self.org_name
class Meta:
db_table = u'organization'
class Server(models.Model):
server_id = models.AutoField(primary_key = True)
server_name = models.CharField(max_length = 135,verbose_name =Server Name)
org = models.ForeignKey(Organization,verbose_name =Organization)
def __unicode __(self) b $ b return self.server_name
class Meta:
db_table = u'server'
admin.py:
class ServerAdmin(admin。 ModelAdmin):
list_display =('server_name','org')
admin.site.register(Server,ServerAdmin)
现在我希望这段代码可以在 ChangeList View
中显示组织名称,但是我得到这个: p>
如果我删除 list_display
ServerAdmin中的
class,我得到这个: org
我没有修改模板或覆盖任何 ModelAdmin
方法。我正在使用Mysql(5.1.58)作为ubuntu 11.10存储库附带的数据库。
如果我能为此而轻而易举,我会很高兴问题家伙感谢提前。
我第二个 Stefano 的事实是 null = True,blank = True
被添加。但是,我认为您只需将其添加到 Organization
模型的 org_name
字段中。这应该通过你的方式。它必须完成,因为您已经运行 inspectdb
从您的旧数据库创建模型。并且可能在DB中的组织
表中存储一个空字符串。所以,添加以上将允许管理员显示一个空白字段/列。
此外,您还可以尝试使用回调,在您不想对其进行更改的情况下模型定义如上所述。
I've got a weird problem in django admin list_display
. Whenever I add a foreign key to a list_display
the whole change list view goes blank showing only the total no of entries.
models.py:
class Organization(models.Model):
org_id = models.AutoField(primary_key=True)
org_name = models.CharField(max_length=288)
def __unicode__(self):
return self.org_name
class Meta:
db_table = u'organization'
class Server(models.Model):
server_id = models.AutoField(primary_key=True)
server_name = models.CharField(max_length=135,verbose_name="Server Name")
org = models.ForeignKey(Organization,verbose_name="Organization")
def __unicode__(self):
return self.server_name
class Meta:
db_table = u'server'
admin.py:
class ServerAdmin(admin.ModelAdmin):
list_display = ('server_name','org')
admin.site.register(Server,ServerAdmin)
Now I'd expect this code to show me the organization name in the ChangeList View
, But instead I get this:
If I remove the org
in the list_display
of ServerAdmin
class, I get this:
I didn't modify the template or override any ModelAdmin
methods. I'm using Mysql(5.1.58) as my database that comes with ubuntu 11.10 repository.
I'll be really glad if I could a get a sloution for this problem guys. Thanks in advance.
I second Stefano on the fact that null=True, blank=True
is to be added. But, I think you only need to add it to the org_name
field of the Organization
model. That should make your way through. It has to be done because you have run inspectdb
to create models from your legacy DB. And probably the organization
table in the DB has an empty string stored. So, adding the above would allow the Admin to have a blank field/column displayed.
Moreover, you can also try using callbacks in situations where you don't want to make changes to your model definition like the above.
这篇关于Django管理列表显示+ ForeignKey =空变更列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!