Django update one field using ModelForm

You could use a subset of the fields in your ModelForm by specifying those fields as follows:

class PartialAuthorForm(ModelForm):
    class Meta:
        model = Author
        fields = ('name', 'title')

From the docs:

If you specify fields or exclude when creating a form with ModelForm, then the fields that are not in the resulting form will not be set by the form's save() method.

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form


Got this figured. What I do is update the request.POST dictionary with values from the instance - so that all unchanged fields are automatically present. This will do it:

from django.forms.models import model_to_dict
from copy import copy

def UPOST(post, obj):
    '''Updates request's POST dictionary with values from object, for update purposes'''
    post = copy(post)
    for k,v in model_to_dict(obj).iteritems():
        if k not in post: post[k] = v
    return post

and then you just do:

form = CModelForm(UPOST(request.POST,c_instance),instance=c_instance)

Tags:

Python

Django