[Django]-Django – Save modelForm even if no field are changed on POST

5👍

If the data for the models hasn’t been altered, the model isn’t marked as dirty, so I’m presuming the formset.save(commit=False) returns Null. But you know the data is dirty, so why not just do this:

if formset.is_valid():
    for form in formset:
        instance = form.instance
        instance.position = pk_list.index(instance.layer.pk)
        instance.save()

In other words, you just obtain the model instance from each bound form, you don’t rely on the save method to answer them.

7👍

There is another way, how to to this:

class LayerMapOptionsForm(forms.ModelForm):
    def has_changed(self, *args, **kwargs):
        return True

Leave a comment