4

我有以下代码:

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

我的问题是:

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

1 回答 1

6

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

这里的 try-except 模式是为了防止AttributeError我自己没有见过的极端情况,但是当我们尝试访问无法验证cleaned_data的表单 (in ) 的属性时显然会出现这种情况。从https://stackoverflow.com/a/877920/492075self.forms了解到这一点

(注意:我的项目仍在 Django 1.3 上;在 1.4 中没有尝试过)

于 2013-01-27T02:51:00.660 回答