Django ChoiceField

后端 未结 4 1294
醉话见心
醉话见心 2020-12-02 10:42

I\'m trying to solve following issue:

I have a web page that can see only moderators. Fields displayed on this page (after user have registered):
Username, Firs

4条回答
  •  离开以前
    2020-12-02 11:09

    New method in Django 3

    you can use Field.choices Enumeration Types new update in django3 like this :

    from django.db import models
    
    class Status(models.TextChoices):
        UNPUBLISHED = 'UN', 'Unpublished'
        PUBLISHED = 'PB', 'Published'
    
    
    class Book(models.Model):
        status = models.CharField(
            max_length=2,
            choices=Status.choices,
            default=Status.UNPUBLISHED,
        )
    

    django docs

提交回复
热议问题