【发布时间】:2012-12-30 00:07:20
【问题描述】:
我有以下代码:
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方法中访问表单集的“父”表单?
【问题讨论】:
-
很抱歉,我不能投票赞成这个问题,但我不能这样做,因为它已被回答,但答案尚未被接受。
标签: django validation django-forms django-admin