0

Kind of duplicate of both of these:

But I am still wondering is there any better way to do it in any later Django updates?

I aimed to modify my model admin stacked inline objects (pre or post save doesn't matter) when parent model admin instance is changed. I tried overriding save_formset and save_related in model admin class as written in Django docs here and here using commit=False. For example:

def save_formset(self, request, form, formset, change):
    instances = formset.save(commit=False)
    for instance in instances:
       instance.any_field = "value"
       instance.save()

The method formset.save(commit=False) returns inline form instances only if they are changed on Django Admin panel as mentioned in django docs, but it doesn't return list of inline objects if they are not changed, even after sending commit=False. I need to edit those inline objects regardless if they were changed on admin panel or not.

1 Answers1

0

I couldn't find a legit way of pre-save processing these unchanged inline forms. So I ended up doing post save processing.

def save_formset(self, request, form, formset, change):
    super().save_formset(request, form, formset, change)
    forms = formset.forms
    for form in forms:
        obj = form.instance
        # Do anything with obj
        obj.save()