我想知道在验证父级时是否有办法访问内联表单。例如,如果我的设置如下所示:

admin.py

class ChildInline(nested_admin.NestedTabularInline):
    form = forms.ChildInlineForm
    model = models.Child
    extra = 0


@admin.register(models.Parent)
class ParentAdmin(nested_admin.NestedModelAdmin):
    form = forms.ParentForm
    inlines = [ChildInline]

模型.py
class Parent(models.Model):
    name = models.CharField(max_length=10)

class Child(models.Model):
    name = models.CharField(max_length=10)
    parent = models.ForeignKey(
        Parent, on_delete=models.CASCADE, related_name='children'
    )

forms.py
class ChildForm(forms.ModelForm):
    class Meta:
        model = models.Child
        fields = '__all__'


class ParentForm(forms.ModelForm):
    class Meta:
        model = models.Parent
        fields = '__all__'

    def clean(self):
        super().clean()
        # How would I access the ChildForm here?

有没有办法从 ChildForm 访问 ParentForm.clean() 我意识到我可以在 child 中获取父数据 - 但我的用例涉及多个有数据传递给父级的 child - 我想在顶层验证(如果可能的)。

我也尝试过在 model.Parent.clean() 方法中进行验证,但是 Child 模型是在父模型之后保存的,所以这似乎也不是初学者。

离开下面的答案,我也尝试使用自定义表单集:
class ParentFormSet(forms.BaseModelFormSet):

    def clean(self):
        if any(self.errors):
            return
        for f in self.forms:
            print(f.cleaned_data)

class ParentForm(forms.ModelForm):
    class Meta:
        model = models.Parent
        fields = '__all__'
    formset = ParentFormSet

但是没有打印任何内容,因此似乎无法访问它?

最佳答案

您也许能够覆盖 ModeAdmin 方法 save_formset()
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset


@admin.register(models.Parent)
class ParentAdmin(nested_admin.NestedModelAdmin):
    form = forms.ParentForm
    inlines = [ChildInline]

    def save_formset(self, request, form, formset, change):
        parent_form = form
        for form_set_form in formset:
            if formset_form.is_valid():
                form_set_form_data = form_set_form.cleaned_data
                # do some validation here...
        formset.save()



关于Django 表单验证 : get child form data in parent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59734409/

10-15 14:21