1

I do not understand the official document about exclude .

Set the exclude attribute of the ModelForm‘s inner Meta class to a list of fields to be excluded from the form.

For example:

class PartialAuthorForm(ModelForm):
    class Meta:
        model = Author
        exclude = ['title']
Since the Author model has the 3 fields name, title and birth_date, this will result in the fields name and birth_date being present on the form.


My understanding is as follows: django form save method will save all form data.If one set exclude =('something',) , 'something' field will not show on frontend and wouldn't be save while calling form save method.
But when I do as the document saying, 'something' field still show.What's the matter?

I also want to add some fields to a form for validating which can show on frontend without saving.It is stange that I find nothing about this need.



**update**

my code :

class ProfileForm(Html5Mixin, forms.ModelForm):

    password1 = forms.CharField(label=_("Password"),
                                widget=forms.PasswordInput(render_value=False))
    password2 = forms.CharField(label=_("Password (again)"),
                                widget=forms.PasswordInput(render_value=False))

    captcha_text = forms.CharField(label=_("captcha"),
                                widget=forms.TextInput())
    captcha_detext = forms.CharField(
                                widget=forms.HiddenInput())

    class Meta:
        model = User
        fields = ("email", "username")
        exclude = ['captcha_text']

    def __init__(self, *args, **kwargs):
        super(ProfileForm, self).__init__(*args, **kwargs)
        ..........

    def clean_username(self):
        .....

    def clean_password2(self):
        ....

    def save(self, *args, **kwargs):
        """
        Create the new user. If no username is supplied (may be hidden
        via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or
        ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so
        that if profile pages are enabled, we still have something to
        use as the profile's slug.
        """
        ..............

    def get_profile_fields_form(self):
        return ProfileFieldsForm

if exclude only affect the model defined under class Meta , so exclude = ['captcha_text'] would not work?

Mithril
  • 12,947
  • 18
  • 102
  • 153

2 Answers2

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
  • 4,773
  • 2
  • 27
  • 42
  • So,the save method is only be used to save the `model` defined under **class Meta** ,and extra_field would not be saved? – Mithril Jul 15 '13 at 08:47
  • Since extra_field is not there in the model itself, where do you expect the extra_field to get saved? – Sudipta Jul 15 '13 at 08:58
  • Thank you!I get that now. It's really a hard way to do some modification on a large django project... – Mithril Jul 15 '13 at 09:05
  • what if I want to supply `title` at time of saving. Should I put the data in `cleaned_data['title'] = 'something'` – Sidhin S Thomas Jun 27 '17 at 14:06
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.

Community
  • 1
  • 1
Philipp Zedler
  • 1,660
  • 1
  • 17
  • 36