Search code examples
djangovalidationdjango-formsdjango-admininline-formset

validation between django inline formsets


I am surprised that this question has not been asked before (or at least I have not found it). I have a ModelAdmin with two inline formsets, and would like to do a cross-validation between them.

class PublicationGroupInlineFormSet(BaseInlineFormSet):
    def clean(self):
        # Here I validate the PublicationGroupInlineFormSet 

class PublicationGroupInline(StackedInline):
    model = PublicationGroup
    formset = PublicationGroupInlineFormSet

class PublicationProjectInlineFormSet(BaseInlineFormSet):
    def clean(self):
        # Here I validate the PublicationProjectInlineFormset 

class PublicationProjectInline(StackedInline):
    model = PublicationProject
    formset = PublicationProjectInlineFormSet

class PublicationAdmin(ModelAdmin):
    inlines = (PublicationProjectInline, PublicationGroupInline)

admin.site.register(Publication, PublicationAdmin)

The question is simple, the answer may not. How can I do cross-validation betwen both formsets? Thanks!


Solution

  • Luckily there was another user who had the same need and hacked a solution https://stackoverflow.com/a/2746735

    Basically the solution consists on overwriting add_view and change_view from admin.ModelAdmin to include cross validation between formsets.