Django admin validation for inline form which rely on the total of a field between all forms Django admin validation for inline form which rely on the total of a field between all forms django django

Django admin validation for inline form which rely on the total of a field between all forms


I know the question was asked a long time ago, but since I struggled with the same problem, I think it might be usefull.

The key here is to define a custom formset to embed into the tabular admin form, then to override the formset clean's method.

Here's an example : a composition is made of composition_elements, each composition_element has a percent field, and I want to validate that the total percent is equal to 100.

from django import formsfrom django.forms.models import BaseInlineFormSetfrom django.core.exceptions import ValidationErrorfrom django.utils.translation import ugettext_lazy as _from django.contrib import adminfrom .models import Composition, CompositionElementclass CompositionElementFormSet(BaseInlineFormSet):    '''    Validate formset data here    '''    def clean(self):        super(CompositionElementFormSet, self).clean()        percent = 0        for form in self.forms:            if not hasattr(form, 'cleaned_data'):                continue            data = form.cleaned_data            percent += data.get('percent', 0)        if percent != 100:            raise ValidationError(_('Total of elements must be 100%%. Current : %(percent).2f%%') % {'percent': percent})class CompositionElementAdmin(admin.TabularInline):    model = CompositionElement    formset = CompositionElementFormSetclass CompositionAdmin(admin.ModelAdmin):    inlines = (CompositionElementAdmin,)admin.site.register(Composition, CompositionAdmin)