djangoフォームセットのすべてのフォームを必須にする方法は? を使用してRequiredFormSet
います。しかし、うまくいきません。何か不足していますか?ありがとう
class RequiredFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(RequiredFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
#self.forms[0].empty_permitted = False
class RecipeIngredientForm(CustomModelForm):
class Meta:
model = RecipeIngredient
def clean(self):
ingredient = self.cleaned_data['ingredient']
unit = self.cleaned_data['unit']
if ingredient.ing_type in (ingredient.TYPE_WET, ingredient.TYPE_DRY) and not unit:
raise forms.ValidationError('unit missing for %s.' % ingredient.name)
return self.cleaned_data
RecipeIngredientFormSet = formset_factory(RecipeIngredientForm, formset=RequiredFormSet)