Django - 如何强制用户在复选框模型中 select 一定数量的选项?
Django - how to force user to select a certain amount of options in a checbox 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;"})
}
现在假设我想确保用户必须 select 最少 2 个水果,但最多 3 个。我将如何在后端执行此操作?
您可以覆盖表单的 clean
方法来检查是否检查了两到三个 BooleanField
:
from django.core.exceptions import <strong>ValidationError</strong>
class FruitPicker(ModelForm):
# ⋮
def <strong>clean</strong>(self, *args, **kwargs):
data = super().clean(*args, **kwargs)
total = sum([data[x] for x in ['banana', 'grape', 'apple', 'mango']])
if not (2 <= total <= 3):
raise ValidationError('You should select between 2 and 3 fruits.')
return data
这里sum(…)
对bool
ean求和,False
用0
,True
用1,这样算出True
秒。如果该数字不在 2
和 3
之间,则会引发 ValidationError
.
我正在做一个围绕模型构建的简单调查:
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;"})
}
现在假设我想确保用户必须 select 最少 2 个水果,但最多 3 个。我将如何在后端执行此操作?
您可以覆盖表单的 clean
方法来检查是否检查了两到三个 BooleanField
:
from django.core.exceptions import <strong>ValidationError</strong>
class FruitPicker(ModelForm):
# ⋮
def <strong>clean</strong>(self, *args, **kwargs):
data = super().clean(*args, **kwargs)
total = sum([data[x] for x in ['banana', 'grape', 'apple', 'mango']])
if not (2 <= total <= 3):
raise ValidationError('You should select between 2 and 3 fruits.')
return data
这里sum(…)
对bool
ean求和,False
用0
,True
用1,这样算出True
秒。如果该数字不在 2
和 3
之间,则会引发 ValidationError
.