Display field choice as part of model string name

后端 未结 1 1508
囚心锁ツ
囚心锁ツ 2020-12-31 00:19

From the django documentation, what if I had

GENDER_CHOICES = (
    (\'M\', \'Male\'),
    (\'F\', \'Female\'),
)

class Person(models.Model):
    name = mod         


        
相关标签:
1条回答
  • 2020-12-31 00:44

    Use get_gender_display():

    return u"%s [%s]" % (self.name, self.get_gender_display())
    

    Note, if you're not using Python 3+ you should be defining __unicode__ rather than __str__.

    0 讨论(0)
提交回复
热议问题