Meghdeep Ray
Meghdeep Ray

Reputation: 5537

Django admin screen doesn't show entities by name

The Admin screen

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 ?

Add User

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

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

Upvotes: 4

Views: 980

Answers (1)

Celeo
Celeo

Reputation: 5682

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)

Upvotes: 4

Related Questions