Skip to content Skip to sidebar Skip to footer

How To Use Admin.tabularinline In A Modelform

full story Only validate admin form if condition is true I am trying to validate that a form has an answer. In order to validate a form in the admin you need to make a new form and

Solution 1:

This is what I ended up doing based on Django admin validation for inline form which rely on the total of a field between all forms

class CombinedFormSet(BaseInlineFormSet):
    # Validate formset data here
    def clean(self):
        super(CombinedFormSet, self).clean()
        for form in self.forms:
            if not hasattr(form, 'cleaned_data'):
                continue

            data = self.cleaned_data
            valid = False
            for i in data:
                if i != {}:
                    if i['is_correct']:
                        valid = True

            if not valid:
                #TODO: translate admin?
                raise forms.ValidationError("A Question must have an answer.")

            # Always return the cleaned data, whether you have changed it or
            # not.
            return data


class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 4
    formset = CombinedFormSet

Post a Comment for "How To Use Admin.tabularinline In A Modelform"