Django表单验证取决于表单集中的数据

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'])访问表单集,但事实并非如此。

我的问题是:

  1. 如何访问表单集?
  2. 我是否需要使用自定义验证来创建自定义表单集才能正常工作?
  3. 如果需要这样做,如何在其clean方法中访问表单集的“父”形式?

Chr*_*ink 5

我刚刚为自己的项目解决了这个问题。确实,正如您在第二个问题中所建议的那样,要求访问父表单的任何内联表单集验证都必须在子类的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中尝试过)