Name of the object not coming in Django admin

I’ve included str but name & price not showing in admin
Instead Item Model Object (2)
“” “” “” (1) & so on is coming.
Where is the error

from django.db import models

# Create your models here.

class ItemModel(models.Model):
    id = models.IntegerField(primary_key = True)
    name = models.CharField(max_length = 80,blank = False)
    price = models.FloatField()

class Meta:
    ordering = ['name']

    def __str__(self):
	    return f"{self.name}:{self.price}"

Is your identation correct?
In the provided code, there’s 2 classes: ItemModel and Meta. The __str__ method is inside the Meta.

Normally you would have this like:

from django.db import models

# Create your models here.

class ItemModel(models.Model):
    id = models.IntegerField(primary_key = True)
    name = models.CharField(max_length = 80,blank = False)
    price = models.FloatField()

    class Meta:
        ordering = ['name']

    def __str__(self):
	    return f"{self.name}:{self.price}"
1 Like