我有以下代码:
from django import forms
from django.core.exceptions import ValidationError
class MyAdminForm(forms.ModelForm):
class Meta:
model = MyModel
def clean(self):
cleaned_data = self.cleaned_data
max_num_items = cleaned_data['max_num_items']
inline_items = cleaned_data.get('inlineitem_set', [])
if len(inline_items) < 2:
raise ValidationError('There must be at least 2 valid inline items')
if max_num_items > len(inline_items):
raise ValidationError('The maximum number of items must match the number of inline items there are')
return cleaned_data
我以为我可以从cleaned_data
(通过使用cleaned_data['inlineitem_set']
)访问表单集,但似乎并非如此。
我的问题是:
- 如何访问表单集?
- 我是否需要使用自定义验证创建自定义表单集才能使其正常工作?
- 如果我需要这样做,如何在其
clean
方法中访问表单集的“父”表单?