[Answer]-ValidationError when modifying an object with inline admin

1👍

I encountered exactly the same issue.
Finally solved it by not implementing the create/delete logic on the Model-side but in the Forms/FormSets.

So in the ‘main’ admin class, in your case ‘EmplacementAdmin’, extended the save_formset method:

def save_formset(self, request, form, formset, change):
    instance = form.instance
    deleted = []
    if instance and instance.__original_num_position != instance.num_position:
        ''' create new Positions if needed '''

        formset.delete_positions = None # pass on a list, QuerySet, whatever

    super(EmplacementAdmin, self).save_formset(request, form, formset, change)

And create a PositionFormSet like so:

class PositionFormSet(BaseInlineFormSet):
    delete_positions = []

    @property
    def deleted_forms(self):
        deleted_forms = []
        try:
            deleted_forms = super(PositionFormSet, self).deleted_forms
        except AttributeError:
            pass
        for form in self.forms:
            if form.instance in self.delete_positions:
                 deleted_forms.append(form)
        self.delete_positions = []
        return deleted_forms

And set this formset in your inlineadmin:

class PositionInline(admin.TabularInline):
    model = Position
    formset = PositionFormSet

Not exactly the way I had in mind, but the trick worked for me. Curious to know if there are some more elegant solutions 🙂

Leave a comment