Vlad
Vlad

Reputation: 127

Django blog administration

I've managed to make a simple blog app in django using some tutorials, however there's one thing I'd like to change: when viewing all posts using admin panel, all of them are named Post object. Is there a way to fix it so there's a title of a post instead of Post object?

Admin panel

Post model:

class Post(models.Model):
    title = models.CharField(max_length=255)
    datetime = models.DateTimeField(u'Date of publishing')
    content = models.TextField(max_length=10000)

Upvotes: 1

Views: 124

Answers (1)

Alex Parakhnevich
Alex Parakhnevich

Reputation: 5172

You need to implement a __unicode__ method on your model.

class Post(models.Model):
    title = models.CharField(max_length=255)
    datetime = models.DateTimeField(u'Date of publishing')
    content = models.TextField(max_length=10000)

    def __unicode__(self):
        return self.title

Note that in case you're using Python 3, you should use __str__ instead.

Docs on this: link

Upvotes: 6

Related Questions