使Django管理员显示主键而不是每个对象的对象类型 [英] Making Django admin display the Primary Key rather than each object's Object type

查看:159
本文介绍了使Django管理员显示主键而不是每个对象的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django 1.1管理员中,当我添加或更改对象时,我的对象显示为:

In Django 1.1 admin, when I go to add or change an object, my objects are displayed as:

Select host to change
    * Add host

    Host object
    Host object
    Host object
    Host object
    Host object

这种情况适用于我网站上的所有型号,而不仅仅是Hosts。

This happens for all models in my site, not just Hosts.

而不是为每个对象显示相同的名称,我希望Django显示主键。

Rather than display the same name for each object, I would like Django to display the primary key.

Select host to change
    * Add host

    machine1
    machine2

这是我的代码:

from django.db import models

# Create your models here.

class Host(models.Model):
    host = models.CharField(max_length=100,primary_key=True)
    class Admin:
        list_display = ('host')


class Test(models.Model):
    testname = models.CharField(max_length=100,primary_key=True)
    class Admin:
        list_display = ('testname')

class Result(models.Model):
    host = models.ForeignKey(Host)
    TESTRESULT_CHOICES = (
        ('P', 'Pass'),
        ('F', 'Fail'),
    )
    testresult = models.CharField(max_length=1, choices=TESTRESULT_CHOICES)
    reason = models.CharField(max_length=100)
    time = models.DateTimeField()
    testname = models.OneToOneField(Test, primary_key=True)
    class Admin:
        list_display = ('host','testname','time','testresult','reason')

阅读< a href =http://docs.djangoproject.com/en/dev/ref/contrib/admin /rel =noreferrer> http://docs.djangoproject.com/en/dev/ref/contrib/admin/ :

ModelAdmin .list_display

"ModelAdmin.list_display

设置list_display来控制管理员的更改列表页面上显示哪些字段。

Set list_display to control which fields are displayed on the change list page of the admin."

然而,这根本似乎不起作用。我做错了吗?

However this simply does not seem to work. Am I doing something wrong?

推荐答案

添加 __ unicode __() 方法到主机。要显示您的主机对象的主键,您可能需要:

Add a __unicode__() method to Host. To show the primary key of your host objects, you'd want something like:

class Host(models.Model):
    host = models.CharField(max_length=100, primary_key=True)

    def __unicode__(self):
        return self.pk

    ...

您可能想考虑显示主机的内容 field:

You might want to think about showing the contents of the host field:

class Host(models.Model):
    host = models.CharField(max_length=100, primary_key=True)

    def __unicode__(self):
        return self.host

    ...

您需要为每个型号做类似的事情。

You'll need to do something similar for every model you've got.

Python 3的兼容性,你会想做这样的事情(见文档):

For Python 3 compatibility, you'll want to do something like this (see the documentation):

from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Host(models.Model):
    host = models.CharField(max_length=100, primary_key=True)

    def __str__(self):
        return self.host

    ...

这篇关于使Django管理员显示主键而不是每个对象的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆