default value for django choice field

前端 未结 2 1289
天涯浪人
天涯浪人 2020-12-17 17:43

Suppose class Photo is to hold photos having choice fields and other attributes:

class Photo(models.Model):
    ONLYME = \'O\'
    FRIENDS =         


        
相关标签:
2条回答
  • 2020-12-17 18:32

    You could just set the default attribute:

    display = models.CharField(default='F', max_length=1, choices=CHOICES, blank=True, null=True)
    

    Reference.

    0 讨论(0)
  • 2020-12-17 18:47

    Use the default attribute:

    display = models.CharField(..., default=FRIENDS)
    

    or

    display = models.CharField(..., default=CHOICES[1][1])
    
    0 讨论(0)
提交回复
热议问题