I'm making a simple survey built around a modelform:
models.py
class Fruits(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200, default='')
text = models.TextField(default='')
banana = models.BooleanField(default=False)
grape = models.BooleanField(default=False)
apple = models.BooleanField(default=False)
mango = models.BooleanField(default=False)
forms.py
class FruitPicker(ModelForm):
class Meta:
model = Fruits
fields = [
'title',
'text',
'banana',
'grape',
'apple',
'mango'
]
widgets = {
'text': forms.Textarea(attrs={"style": "height:10em;" "width:60em;"}),
'banana': forms.CheckboxInput(attrs={"style": "margin-left:350px;"}),
'grape': forms.CheckboxInput(attrs={"style": "margin-left:350px;"}),
'apple': forms.CheckboxInput(attrs={"style": "margin-left:350px;"}),
'mango': forms.CheckboxInput(attrs={"style": "margin-left:350px;"})
}
Now let's say I want to make sure the user has to select a minimum amount of 2 fruits, but a maximum amount of 3. How would I go about doing this on the back-end?