How to properly use the “choices” field option in Django

前端 未结 7 2115
孤独总比滥情好
孤独总比滥情好 2020-12-12 23:17

I\'m reading the tutorial here: https://docs.djangoproject.com/en/1.5/ref/models/fields/#choices and i\'m trying to create a box where the user can select the month he was b

7条回答
  •  [愿得一人]
    2020-12-13 00:03

    The cleanest solution is to use the django-model-utils library:

    from model_utils import Choices
    
    class Article(models.Model):
        STATUS = Choices('draft', 'published')
        status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20)
    

    https://django-model-utils.readthedocs.io/en/latest/utilities.html#choices

提交回复
热议问题