Aminah Nuraini
Aminah Nuraini

Reputation: 19206

Django - CheckboxSelectMultiple shows object representation instead of object's name

So I am trying to have a list of checkboxes of cities, but instead of showing the cities' name, it shows this:

Wrong print

How to make it shows the name instead of City object?

Upvotes: 0

Views: 798

Answers (1)

sebb
sebb

Reputation: 1966

In your model you have to include __str__ for python3 and unicode for python 2

For example python 3:

class City(models.Model):
    name = forms.CharField(max_length=200, default="")

    def __str__(self):
        return self.name

Python 2

class City(models.Model):
    name = forms.CharField(max_length=200, default="")

    def __unicode__(self):
        return self.name

Upvotes: 2

Related Questions