django - 如何交叉检查 ModelAdmin 及其内联? [英] django - How to cross check ModelAdmin and its inlines?

查看:20
本文介绍了django - 如何交叉检查 ModelAdmin 及其内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型(ModelParent 和 ModelChild)在主题模型上具有相同的 m2m 字段.ModelChild 在 ModelParent 上有一个外键,ModelChild 在管理页面上被定义为 ModelParent 的内联.

I have two models (ModelParent and ModelChild) with same m2m fields on Subject model. ModelChild has a foreign key on ModelParent and ModelChild is defined as inline for ModelParent on admin page.

### models.py ###
  class Subject(Models.Model):
    pass

  class ModelParent(models.Model):
    subjects_parent = ManyToManyField(Subject)

  class ModelChild(models.Model):
    parent = ForeignKey(ModelParent)
    subjects_child = ManyToManyField(Subject)

### admin.py ###
  class ModelChildInline(admin.TabularInline):
      model = ModelChild

  class ModelParentAdmin(admin.ModelAdmin):
    inlines = [ModelChildInline]

  admin.site.register(ModelParent, ModelParentAdmin)

不过,我有一个重要的限制,ModelChild 的 subject_child 字段不得引用 subject_parent 与其 subject_parent 所做的任何主题.

I have one important restriction though, ModelChild's subjects_child field must not reference any subject that subject_parent does with its subjects_parent.

因此,如果我在管理页面上为两个模型选择相同的主题(在 subject_parent 和 subject_child 中),我该如何验证?如果只有一个字段发生变化,您会根据数据库对其进行验证,但如果两者都发生变化(subject_parent 和 subject_child)怎么办?如何在保存前同时验证两个表单?

So, if I select the same Subject (in subject_parent and subject_child) on Admin page for both models, how can I validate this? If only one field changes you validate it against the db, but what if both change (subject_parent and subject_child)? How can I validate both forms together before saving?

推荐答案

我从 admin.ModelAdmin 继承了一个名为 ModelAdminWithInline 的新类,并修改了 add_view(...) 和 change_view(...) 方法来调用函数 is_cross_valid(self、form、formsets),您可以在其中一起验证所有表单.两个函数都有:

I have inherited a new class named ModelAdminWithInline from admin.ModelAdmin and modified methods add_view(...) and change_view(...) to call function is_cross_valid(self, form, formsets), where you can validate all the forms together. Both functions had:

#...
if all_valid(formsets) and form_validated:
#...

改为:

#...
formsets_validated = all_valid(formsets)
cross_validated = self.is_cross_valid(form, formsets)
if formsets_validated and form_validated and cross_validated:
#...

新函数 is_cross_valid(...) 定义如下:

The new function is_cross_valid(...) is defined like this:

def is_cross_valid(self, form, formsets):
  return True

因此,如果您不更改 is_cross_valid(...) 函数,新类的工作方式应该与 ModelAdmin 完全相同.

so the new class should work exactly the same as ModelAdmin if you don't change is_cross_valid(...) function.

现在我的 admin.py 看起来像这样:

Now my admin.py looks like this:

###admin.py###
class ModelAdminWithInline(admin.ModelAdmin):
  def is_cross_valid(self, form, formsets):
    return True

  def add_view(self, request, form_url='', extra_context=None):
    #modified code

  def change_view(self, request, object_id, extra_context=None):
    #modified code

class ModelChildInline(admin.TabularInline):
  model = ModelChild

class ModelParentAdmin(ModelAdminWithInline):
  inlines = [ModelChildInline]

  def is_cross_valid(self, form, formsets):
    #Do some cross validation on forms
    #For example, here is my particular validation:
    valid = True

    if hasattr(form, 'cleaned_data'):   

      subjects_parent = form.cleaned_data.get("subjects_parent")

      #You can access forms from formsets like this:
      for formset in formsets:
        for formset_form in formset.forms:
          if hasattr(formset_form, 'cleaned_data'):

            subjects_child = formset_form.cleaned_data.get("subjects_child")
            delete_form = formset_form.cleaned_data.get("DELETE")

            if subjects_child and (delete_form == False):
              for subject in subjects_child:
                if subject in subjects_parent:
                  valid = False
                  #From here you can still report errors like in regular forms:
                  if "subjects_child" in formset_form.cleaned_data.keys():
                    formset_form._errors["subjects_child"] = ErrorList([u"Subject %s is already selected in parent ModelParent" % subject])
                    del formset_form.cleaned_data["subjects_child"]
                  else:
                    formset_form._errors["subjects_child"] += ErrorList(u"Subject %s is already selected in parent ModelParent" % subject])

      #return True on success or False otherwise.
      return valid

admin.site.register(ModelParent, ModelParentAdmin)

解决方案有点hackish,但它有效:).错误显示与常规 ModelForm 和 ModelAdmin 类相同.Django 1.2(即将发布)应该有模型验证,所以我希望这个问题能得到更好的解决.

The solution is a little bit hackish but it works :). The errors show up the same as with regular ModelForm and ModelAdmin classes. Django 1.2 (which should be released shortly) should have model validation, so I hope that then this problem could be solved more nicely.

这篇关于django - 如何交叉检查 ModelAdmin 及其内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆