nickk2002
nickk2002

Reputation: 59

Change Django model name indexing in the admin view

Whether I am trying to access all the instances of "Project" model class from the admin page or in the python shell I still get (Project.objects.all() in pyhon shell or in admin : https://ibb.co/S7F491d)

<Project: Project object (4)>, <Project: Project object (5)>, <Project: Project object (6)>

but instead i want

<Project: Project object (1)>, <Project: Project object (2)>, <Project: Project object (3)>

the indexing seems to show the ith Project created.

class Project(models.Model):
    title = models.CharField(max_length=20)
    description = models.CharField(max_length=200, blank=True)
    image = models.ImageField(null=True)
    data = models.DateField(blank=False, null=10 / 20 / 2009)
    objects = models.Manager()

Upvotes: 1

Views: 291

Answers (2)

Stevy
Stevy

Reputation: 3387

Your model handles the primary key and auto increments the id column every time a new object is created.

Its a bad idea, but if you really wish to reuse your deleted primary keys then you could switch off auto-increment and manually manage the primary key yourself, but this can lead to errors and requires some effort.

The primary key value itself is only used for IDs and are not used for anything else of value. If you really need to do this for some reason, this post can help you.

Upvotes: 1

mehdi
mehdi

Reputation: 1150

add __str__ method to your models.py and instance's title will show instead of object:

class Project(models.Model):
    title = models.CharField(max_length=20)
    description = models.CharField(max_length=200, blank=True)
    image = models.ImageField(null=True)
    data = models.DateField(blank=False, null=10 / 20 / 2009)
    objects = models.Manager()

    def __str__(self):
         return self.title

Upvotes: 1

Related Questions