简体   繁体   中英

How to create a field inside a field in a model in django?

I have model as follows.

I have 4 choicefields for people which are (single, couple, family, and group). Users can select any one. 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.

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. The same goes for group choice.

Family pic家庭照片

Single单身的

Now my model is:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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