Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 318
Meghdeep Ray Avatar asked Oct 23 '25 10:10

Meghdeep Ray


1 Answers

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)
like image 140
Celeo Avatar answered Oct 26 '25 00:10

Celeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!