Django admin List Display + ForeignKey = Empty Change List

Django admin List Display + ForeignKey = Empty Change List(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)