Set Django IntegerField by choices=… name

后端 未结 10 887
南笙
南笙 2020-12-07 09:40

When you have a model field with a choices option you tend to have some magic values associated with human readable names. Is there in Django a convenient way to set these f

10条回答
  •  天涯浪人
    2020-12-07 10:04

    Do as seen here. Then you can use a word that represents the proper integer.

    Like so:

    LOW = 0
    NORMAL = 1
    HIGH = 2
    STATUS_CHOICES = (
        (LOW, 'Low'),
        (NORMAL, 'Normal'),
        (HIGH, 'High'),
    )
    

    Then they are still integers in the DB.

    Usage would be thing.priority = Thing.NORMAL

提交回复
热议问题