[Fixed]-Django displays `TourCategory object` string instead of TourCategory's name

1👍

Python uses Class’ __unicode__ method (or __str__ method if you are not using unicode) to display object representation of that object.

Django uses this methods to display objects in admin site.

So you have to define or fix __unicode__ (or __str__)

class TourCategory(models.Model):
    name = models.CharField(max_length=200, verbose_name="Name")

    def __unicode__(self):
        return unicode(self.name)

    def __str__(self):
        return self.name

Look here for the documentation . Normally you should not need to cast
self.name to unicode, but I write it so you can test with and without it.

👤Mp0int

Leave a comment