Hello Dan,

I'm not aware of any third party library handling this but a simple way to 
achieve
what you're after would be to subclass ModelForm in order to override save()
and pass commit=False in your super() call.

e.g. (untested)

class UpdateFieldsModelForm(ModelForm):
    update_fields = None

    def save(self, commit=True):
        instance = super().save(commit=False)
        if commit:
            update_fields = None if instance._state.adding else 
self.update_fields
            instance.save(update_fields=self.update_fields)
        return instance

Cheers,
Simon

Le mardi 18 juin 2019 11:38:53 UTC-4, Dan Davis a écrit :
>
> So, I recently observed that a Django ModelForm updates all columns when 
> it is updating a model instance. This is appropriate for a typical 
> situation where Django is also in control of the schema.
> I also see that you can control this behavior by passing update_fields 
> <https://docs.djangoproject.com/en/2.2/ref/models/instances/#saving-objects> 
> to the save method, in a particular form:
>
>     self.instance.save(update_fields=self.changed_data)
>
> I have some complicated forms that have non-model fields and many-to-many 
> fields mixed in there.   I'm wondering whether there is a module for Django 
> out there that gives me some base ModelForms that do not update all fields, 
> but only changed fields.  I am asking because I don't want to work hard to 
> handle corner cases if there's something already out there.   What I 
> already have is hard-coded to the form where I've made this change to do 
> only a limited update.
>
> Thanks,
>
> -Dan
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8dd0ebb7-3686-4477-b7ee-0b482312d65c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to