简体   繁体   English

如何在 django 的 model 的字段内创建字段?

[英]How to create a field inside a field in a model in django?

I have model as follows.我有 model 如下。

I have 4 choicefields for people which are (single, couple, family, and group).我有 4 个可供选择的人(单身、夫妻、家庭和团体)。 Users can select any one.用户可以 select 任意一款。 But the problem is when the user selects family, he should also select the number of children and no of adults that are going for the trip.但问题是当用户选择家庭时,他还应该 select 去旅行的儿童人数和成人人数。

Now, how can I have a field in the model such that, I have 4 choices, but for a family choice, I also need an option to select no of adults and no of children.现在,我怎样才能在 model 中有一个字段,这样,我有 4 个选择,但是对于家庭选择,我还需要一个选项 select 没有成人和没有儿童。 The same goes for group choice.群体选择也是如此。

Family pic家庭照片家庭照片

Single单身的单身的

Now my model is:现在我的 model 是:

class CustomBooking(models.Model):
    PEOPLE_CHOICES = (
            ('Single', 'Single',),
            ('Couple', 'Couple',),
            ('Family', 'Family',),
            ('Group', 'Group'),
        )
    AGE_GROUP = (
            ('18-35 yrs', '18-35 yrs',),
            ('36-50 yrs', '36-50 yrs',),
            ('51-64 yrs', '51-64 yrs',),
            ('65+ yrs', '65+ yrs',),
        )
    
    people = models.CharField(max_length=64, choices=PEOPLE_CHOICES, default='Couple')
    geographical_area = models.CharField(max_length=255)
    bookedfor = models.DateField(blank=True)
    age_group = models.CharField(max_length=64, choices=AGE_GROUP, default='18-35 yrs')
    
    trip_title = models.CharField(max_length=255)
    description = RichTextField()
    created_at = models.DateTimeField(auto_now_add=True)


    class Meta:
        ordering = ('created_at',)

Just add the fields and give them a default value.只需添加字段并给它们一个默认值。

class CustomBooking(models.Model):
    ...
    number_of_children = models.IntegerField(default=0)
    number_of_adults = models.IntegerField(default=0)

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

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