Django admin List Display + ForeignKey = Empty Change List(Django admin List Display + ForeignKey = Empty Change List)
问题描述
我在 django admin list_display 中遇到了一个奇怪的问题.每当我向 list_display 添加外键时,整个更改列表视图都会变为空白,仅显示条目总数.
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)
现在我希望这段代码能在 ChangeList 视图 中显示组织名称,但我得到的是:
Now I'd expect this code to show me the organization name in the ChangeList View, But instead I get this:
如果我删除 ServerAdmin 类的 list_display 中的 org,我会得到:
If I remove the org in the list_display of ServerAdmin class, I get this:
我没有修改模板或覆盖任何 ModelAdmin 方法.我使用 Mysql(5.1.58) 作为 ubuntu 11.10 存储库附带的数据库.
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.
推荐答案
我第二个 Stefano 关于要添加 null=True, blank=True 的事实.但是,我认为您只需将其添加到 Organization 模型的 org_name 字段即可.那应该可以通过.必须这样做,因为您已经运行 inspectdb 从旧数据库创建模型.并且数据库中的 organization 表可能存储了一个空字符串.因此,添加上述内容将允许管理员显示一个空白字段/列.
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 admin List Display + ForeignKey = Empty Change List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django admin List Display + ForeignKey = Empty Change List
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
