私は単純なモデルへの1対多の関係を作る必要がありますか?

whitebear:

場合の例では、このような単純なモデルがあります。

class People(models.Model):
    name = models.CharField(unique=True,max_length=255)
    sex = ???

Sex フィールドには、女性、男性またはなし、非常に簡単な選択肢でなければなりません。

しかし、この場合でも、私はモデルクラスを作成する必要がありますSexし、人々に多対1の関係を使用します?

class Sex(models.Model):
    label = models.CharField(unique=True,max_length=255)

それはのための選択ボックスにすることができるsexのメンバーPeopleにすることなくクラスをSexクラス???

Arakkalアブ:

使用choicesオプションとして、

class People(models.Model):
    class GenderChoices(models.TextChoices):
        MALE = 'Male'
        FEMALE = 'Female'
        NONE = 'None'

    name = models.CharField(unique=True, max_length=255)
    sex = models.CharField(max_length=10, choices=GenderChoices.choices)

更新

あなたの選択肢をより細かく制御を取得するには、この表現を使います

class GenderChoices(models.TextChoices):
    MALE = 'M', 'Male'
    FEMALE = 'F', 'Female'
    NONE = 'N', 'None'

参考:列挙型

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=406335&siteId=1