[Django]-Display custom format name on django admin page

3👍

You should use __str__ instead of user in list_display:

list_display=['__str__']

Otherwise you tell django to show user field. And since User model doen’t have overrided __str__ method you see user’s id.

Also you can just remove list_display attribute. In this case __str__ will be used by default.

1👍

The list_display–(Django doc) support callable methods too. So, define a user(...) method on Model admin as,

class OnlineUsersAdmin(admin.ModelAdmin):
    # a list of displayed columns name.
    list_display = ['user']

    def user(self, instance):
        return instance.__str__()
👤JPG

0👍

Here, inside return you can return only str + str, not str + int. So, you have to convert every int to str

from django.db import models

# Create your models here.


class Fabonacci(models.Model):
    numstr = models.IntegerField()
    terms = models.CharField(max_length=500)

    def __str__(self):
        return "The first " + str(self.numstr) + " terms in fibonacci series : " + self.terms

Leave a comment