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

前端 未结 3 1198
耶瑟儿~
耶瑟儿~ 2021-02-04 11:43

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
            


        
3条回答
  •  猫巷女王i
    2021-02-04 12:15

    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
    

提交回复
热议问题