Django: Display Choice Value

后端 未结 3 945
心在旅途
心在旅途 2020-12-02 04:03

models.py:

class Person(models.Model):
    name = models.CharField(max_length=200)
    CATEGORY_CHOICES = (
        (\'M\', \'Male\'),
              


        
3条回答
  •  萌比男神i
    2020-12-02 04:39

    Others have pointed out that a get_FOO_display method is what you need. I'm using this:

    def get_type(self):
        return [i[1] for i in Item._meta.get_field('type').choices if i[0] == self.type][0]
    

    which iterates over all of the choices that a particular item has until it finds the one that matches the items type

提交回复
热议问题