python 使 Django 管理员显示主键而不是每个对象的对象类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1594436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:39:47  来源:igfitidea点击:

Making Django admin display the Primary Key rather than each object's Object type

pythondjangoadmin

提问by mikemaccana

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

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

Select host to change
    * Add host

    Host object
    Host object
    Host object
    Host object
    Host object

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

我站点中的所有模型都会发生这种情况,而不仅仅是主机。

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

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

Select host to change
    * Add host

    machine1
    machine2

Here is my code:

这是我的代码:

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')

Reading http://docs.djangoproject.com/en/dev/ref/contrib/admin/:

阅读http://docs.djangoproject.com/en/dev/ref/contrib/admin/

"ModelAdmin.list_display

"ModelAdmin.list_display

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

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

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

然而,这似乎根本行不通。难道我做错了什么?

回答by Dominic Rodger

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

添加一个__unicode__()方法到Host. 要显示主机对象的主键,您需要如下内容:

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

    def __unicode__(self):
        return self.pk

    ...

You might want to think about showing the contents of the hostfield:

您可能需要考虑显示host字段的内容:

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.

您需要为您拥有的每个模型做类似的事情。

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

对于 Python 3 兼容性,您需要执行以下操作(请参阅文档):

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

    ...

回答by Cat Plus Plus

contrib.adminhas been reworked in 1.0, and old Adminclasses inside models no longer work. What you need is ModelAdminsubclass in your_application.adminmodule, e.g.

contrib.admin已在 1.0 中重新设计,Admin模型中的旧类不再有效。你需要的是模块中的ModelAdmin子类your_application.admin,例如

from your_application.models import Host
from django.contrib import admin

class HostAdmin(admin.ModelAdmin):
    list_display = ('host',)

admin.site.register(Host, HostAdmin)

Or use __unicode__in the model itself, e.g.

或者__unicode__在模型本身中使用,例如

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

    def __unicode__(self):
        return self.host

回答by ropable

It might also be worth mentioning that, if you are using an auto-incrementing primary key for your models, you will need to coerce it into a string, eg:

还值得一提的是,如果您为模型使用自动递增的主键,则需要将其强制转换为字符串,例如:

def __unicode__(self):
    return str(self.pk)