简体   繁体   中英

Django admin screen doesn't show entities by name

管理员屏幕

In Django I made an entity called User in Models, when I made it visible in the Admin page it shows it only as User Object instead of by the id or name, how can I change this?

How can I change this to something intuitive ? Like the ID or the Name of the user ?

models.py shows:

class User(models.Model):
    user_id = models.CharField(primary_key=True, max_length=200)
    name = models.CharField(max_length=200)
    phone_number = models.CharField(max_length=200)
    user_password = models.CharField(max_length=20)
    email = models.CharField(max_length=200)
    category = ( ('A','First AC'), ('B','Second AC'), ('C','Third AC'), ('D','Sleeper') )

Additionally how come the choices in category do not appear on the Add User screen ?

添加用户

The admin.py for this app[named 'data'] has:

from django.contrib import admin    
from data.models import User
admin.site.register(User)

Include a __str__ method in your model that returns the string that you want to show:

class User(models.Model):
    user_id = models.CharField(primary_key=True, max_length=200)
    name = models.CharField(max_length=200)
    phone_number = models.CharField(max_length=200)
    user_password = models.CharField(max_length=20)
    email = models.CharField(max_length=200)
    category = ( ('A','First AC'), ('B','Second AC'), ('C','Third AC'), ('D','Sleeper') )

    def __str__(self):
        return '{}'.format(self.user_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM