nyergler / nested-formset

Nest Django formsets for multi-level editing.
BSD 3-Clause "New" or "Revised" License
87 stars 21 forks source link

Propagating has_changed down to parent form #11

Closed aidanlister closed 9 years ago

aidanlister commented 10 years ago

Would it make sense to propagate the has_changed value down from grandchildren forms to parent form?

Maybe adding a has_form_or_children_changed() with a better name?

nyergler commented 10 years ago

I think overriding has_changed to return True if any nested form has changed is a reasonable change. Is that something you're willing to put together a test + code for?

aidanlister commented 10 years ago

I implemented this as:

class ParentForm:
    def has_changed(self):
        return any([childform.has_changed() for childform in self.nested])

I'll write a test tonight (hopefully)

nyergler commented 10 years ago

So I'd expect this to live on BaseNestedFormset; if you look at that class, you'll see that it already overrides the save method to first call its own save(), then the nested grandchildren. Keep in mind that the base save call saves the first two levels of forms, since BaseNestedFormset subclasses BaseInlineFormSet.

So I think it's something like:

    def has_changed(self):
        return super(self, ...).has_changed() and any([
            child.nested.has_changed() for child in self
            if 
        ])

At that point I think has_changed will return True if anything has changed in either the base formset or the nested formsets.

aidanlister commented 10 years ago

Oh I see what you're saying -- just don't call the parent's form_valid() and we don't need to muck with the baseclass's save.