次のコードがあります。
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
メソッドでフォームセットの「親」フォームにアクセスするにはどうすればよいですか?