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

情到浓时终转凉″ 提交于 2020-06-18 11:23:45

问题


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

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


回答1:


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


来源:https://stackoverflow.com/questions/39449472/django-checkboxselectmultiple-shows-object-representation-instead-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!