[Solved]-How can I update only certain fields in a Django model form?

9👍

Only use specified fields:

class FirstModelForm(forms.ModelForm):
    class Meta:
        model = TheModel
        fields = ('title',)
    def clean_title(self....

See http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#controlling-which-fields-are-used-with-fields-and-exclude

It is common to use different ModelForms for a model in different views, when you need different features. So creating another form for the model that uses the same behaviour (say clean_<fieldname> methods etc.) use:

class SecondModelForm(FirstModelForm):
    class Meta:
        model = TheModel
        fields = ('title', 'description')

1👍

If you don’t want to update a field, remove it from the form via the Meta exclude tuple:

class Meta:
    exclude = ('title',)

Leave a comment