Django - How to crosscheck ModelAdmin and its inline strings?

I have two models (ModelParent and ModelChild) with the same m2m fields on the Subject model. ModelChild has a foreign key on ModelParent, and ModelChild is defined as being built-in for ModelParent on the 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)

I have one important limitation, although the ModelShild field of subject_child should not refer to any item that subject_parent does with its subject_parent.

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

+5
2

ModelAdminWithInline admin.ModelAdmin add_view (...) change_view (...) is_cross_valid (self, form, formsets), . :

#...
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 (...) :

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

, ModelAdmin, is_cross_valid (...).

admin.py :

###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)

, :). , ModelForm ModelAdmin. Django 1.2 ( ) , , .

+5

admin clean(). . , . ( ModelAdmin), clean() . :

class SomeForm(ModelForm):
  #some code
  def clean(self):
   #some code
class SomeAdminClass(ModelAdmin):
 #some code
 form = SomeForm
 #more code
+2

All Articles