[Answered ]-Django: form save exculde certain field

2đź‘Ť

âś…

exclude = ['title'] will exclude the field from the form, not from the model.
form.save() will try to save the model instance with the available for fields, but model might throw any error pertaining to the missing field.

To add extra fields in model form, do this:

class PartialAuthorForm (ModelForm):
    extra_field = forms.IntegerField()

    class Meta:
        model = Author

    def save(self, *args, **kwargs):
        # do something with self.cleaned_data['extra_field']
        super(PartialAuthorForm, self).save(*args, **kwargs)

But make sure there is no field called “PartialAuthorForm” in the model Author.

👤Sudipta

0đź‘Ť

First, the reason why your title field is still displayed must be somewhere in your view. Be sure that you create your (unbound) form instance like this:

form = PartialAuthorForm()

and try this simple rendering method in the template

{{ form.as_p }}

Second, it should be no problem to add extra fields to a model form, see e.g. this post.

Leave a comment