Django - clean_field()没有被调用管理员内联表单的更新

时间:2013-03-11 02:57:52

标签: django django-forms django-admin

我无法弄清楚为什么没有为在管理视图上更新的内联表单调用clean_field()方法。我的代码似乎很简单(见下面的概要)。

当我通过管理界面(http://admin/..../primary/1/)修改主表单时,正如预期的那样,我看到:

  • Admin.PrimaryAdminForm.clean_myfield()调用
  • Admin.PrimaryAdminForm.clean()调用
  • Model.Primary.clean()调用

但是,当我修改主要内容时,在成员资格的管理员视图(http://admin/..../membership/1/)中将其视为内联,我只看到:

  • Model.Primary.clean()调用

我尝试在以下位置放置“def clean_myfield(self):”方法,但无法看到它从成员内联主要表单中执行:

  • Model.Primary.clean_myfield
  • Admin.PrimaryAdmin.clean_myfield
  • Admin.PrimaryAdminForm.clean_myfield
  • Admin.PrimaryAdminInline.clean_myfield

是否应该在其他地方放置这个clean_myfield代码?

我已经[形式和字段验证] [docs.djangoproject.com/en/dev/ref/forms/validation/#form-and-field-validation]阅读(并重读)了Django文档。覆盖范围,但内联验证没有任何内容。我还阅读了docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin,但对内联特定验证没有帮助。还有其他文件吗?

---> Austin回答提供了一个文档参考:“如果没有指定”(请参阅​​他的链接),这意味着答案。我在这个主题上添加了request to improve个文档。

在进一步试验后,我通过将代码放入Model.Primary.clean()方法找到了一种解决方法:

def clean(self):
    data = self.myfield
    data += "_extra" # not actual cleaning code 
    self.myfield = data

所以问题仍然存在:为什么Model.clean()似乎是唯一放置管理内联表单验证而不是clean_myfield(self)方法的地方?

---> Austin回答。我需要将form = PrimaryAdminForm添加到PrimaryInline。通过此添加,在成员资格表单上更新PrimaryInline myfield时,将调用PrimaryAdminForm.clean_myfield(self)。由于添加了表单引用,代码排序已更新。

代码简介:

没有forms.py文件 - 所有模型都通过管理界面更新

models.py:

class Membership(models.Model):
    name = models.CharField( max_length=NAME_LENGTH,
        null=True, blank=True, verbose_name="Membership Name Tag",
        help_text="Name of membership" )

class Primary(models.Model):
    user = models.OneToOneField(User, verbose_name="User Name")
    membership = models.OneToOneField(Membership, verbose_name="Membership Name")
    myfield = models.CharField("My Field", max_length=20, null=True, blank=True)


    # clean method altered as in Update comment 
    # Why must this be here? Why not in clean_myfield(self)
    def clean(self):
        data = self.myfield
        data += "_extra" # not actual cleaning code 
        self.myfield = data

admin.py:

class MembershipAdminForm(ModelForm):
    class Meta:
        model = Membership

class PrimaryAdminForm(ModelForm):
    class Meta:
        model = Primary

    def clean_myfield(self):
        data = self.cleaned_data['myfield']
        data += "_extra" # not actual cleaning code 
        return unicode(data)

    def clean(self):
        cleaned_data = super(PrimaryAdminForm, self).clean()
        # not actual cleaning code 
        return cleaned_data

# EDIT2: Moved PrimaryInline so it's defined after PrimaryAdminForm 
class PrimaryInline(admin.StackedInline):
    model = Primary
    form = PrimaryAdminForm   #EDIT2 as recommended by Austin
    raw_id_fields = ['user']
    verbose_name_plural = 'Primary Member'
    fieldsets = ((None, {'classes': ('mbship', ),
                         'fields': ('user', 'myfield')}), )

class MembershipAdmin(admin.ModelAdmin):
    form = MembershipAdminForm

    # inlines
    inlines = [PrimaryInline, ]

    fieldsets = ((None, {'classes': ('mbship',),
                         'fields': ('name'), }), )

class PrimaryAdmin(admin.ModelAdmin):
    form = PrimaryAdminForm
    list_display = ('__unicode__', 'user', 'status', 'date_used' )
    search_fields = ['user__first_name', 'user__last_name', 'user__email'] 

    fieldsets = ((None, {'classes': ('mbship',), 
                         'fields': ('user', 'membership', 'myfield'), }), )

    def clean_myfield(self):
        data = self.cleaned_data['myfield']
        data += "_extra" # not actual cleaning code 
        return unicode(data)

1 个答案:

答案 0 :(得分:8)

验证发生在ModelForm个对象上,而不是ModelAdmin个对象上。如果要覆盖任何干净的方法,则必须为每个所需的模型创建自己的ModelForm后代。

在您的示例中,PrimaryInline类未指定formIf not specified,使用的表单为ModelForm,没有任何自定义清理方法。

试试这个:

class PrimaryInline(admin.StackedInline):
    # ... existing code ...
    form = PrimaryAdminForm

现在,您将使用自定义PrimaryAdminForm和相关的clean()方法。