我正在围绕模型进行一项简单的调查:
模型.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)
表格.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;"})
}
现在假设我想确保用户必须选择最少 2 个水果,但最多选择 3 个。我将如何在后端执行此操作?