Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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/:

"ModelAdmin.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?

like image 949
mikemaccana Avatar asked Oct 20 '09 12:10

mikemaccana


People also ask

How do I set primary key in Django?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

What is __ Str__ in Django?

The __str__ method just tells Django what to print when it needs to print out an instance of the any model. It is also what lets your admin panel, go from this. Note: how objects are just plainly numbered.

What is def __ str __( self in Django?

def __str__( self ): return "something" This will display the objects as something always in the admin interface.


3 Answers

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

    ...

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.

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

    ...
like image 99
Dominic Rodger Avatar answered Sep 28 '22 01:09

Dominic Rodger


contrib.admin has been reworked in 1.0, and old Admin classes inside models no longer work. What you need is ModelAdmin subclass in your_application.admin module, e.g.

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.

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

    def __unicode__(self):
        return self.host
like image 27
Cat Plus Plus Avatar answered Sep 28 '22 03:09

Cat Plus Plus


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)
like image 25
ropable Avatar answered Sep 28 '22 03:09

ropable