Rud*_*lah 3 django validation django-forms django-admin
我有以下代码:
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
Run Code Online (Sandbox Code Playgroud)
我以为可以从cleaned_data
(通过使用cleaned_data['inlineitem_set']
)访问表单集,但事实并非如此。
我的问题是:
clean
方法中访问表单集的“父”形式?我刚刚为自己的项目解决了这个问题。确实,正如您在第二个问题中所建议的那样,要求访问父表单的任何内联表单集验证都必须在子类的clean
方法中BaseInlineFormset
。
令人高兴的是,在clean
调用内联表单集之前,已经创建了父表单的实例(如果要修改而不是创建它,则可以从数据库中检索实例),并且可以在那里使用self.instance
。
from django.core.exceptions import ValidationError
class InlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
try:
forms = [f for f in self.forms
if f.cleaned_data
# This next line filters out inline objects that did exist
# but will be deleted if we let this form validate --
# obviously we don't want to count those if our goal is to
# enforce a min or max number of related objects.
and not f.cleaned_data.get('DELETE', False)]
if self.instance.parent_foo == 'bar':
if len(forms) == 0:
raise ValidationError(""" If the parent object's 'foo' is
'bar' then it needs at least one related object! """)
except AttributeError:
pass
class InlineAdmin(admin.TabularInline):
model = ParentModel.inlineobjects.through
formset = InlineFormset
Run Code Online (Sandbox Code Playgroud)
这里的try-except模式可防止出现一个AttributeError
我从未见过的极端情况,但是当我们尝试访问未通过验证cleaned_data
的表单的属性时,显然会出现self.forms
这种情况。从/sf/answers/61454431/了解到这一点
(注:我的项目仍在Django 1.3上;尚未在1.4中尝试过)
归档时间: |
|
查看次数: |
4256 次 |
最近记录: |