简体   繁体   English

我是否需要与简单的 model 建立一对多关系?

[英]Do I need to make one-to-many relationship to simple model?

For example in the case, there is simple model like this.例如在这种情况下,有这样的简单 model。

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

Sex field should be Female,Male or None, very easy choices. Sex字段应该是女性,男性或无,非常容易选择。

However even in this case I need to make model class Sex and use Many-to-One relationship to People??但是,即使在这种情况下,我也需要制作 model class Sex并与人使用多对一关系?

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

Is it possible to make select box for sex member of People class without making Sex Class???是否可以为People class 的sex成员制作 select 框而不制作Sex Class?

Use choices option as,使用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)

UPDATE更新

to get more control over your choices, use this representation要更好地控制您的选择,请使用此表示

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

Reference: Enumeration types参考: 枚举类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM