i not understand official document exclude .
set exclude attribute of modelform‘s inner meta class list of fields excluded form. example: class partialauthorform(modelform): class meta: model = author exclude = ['title'] since author model has 3 fields name, title , birth_date, result in fields name , birth_date being present on form.
understanding follows: django form save method save form data.if 1 set exclude =('something',) , 'something' field not show on frontend , wouldn't save while calling form save method.
when document saying, 'something' field still show.what's matter?
want add fields form validating can show on frontend without saving.it stange find nothing 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 new user. if no username supplied (may hidden via ``accounts_profile_form_exclude_fields`` or ``accounts_no_username``), generate unique username, if profile pages enabled, still have use profile's slug. """ .............. def get_profile_fields_form(self): return profilefieldsform
if exclude affect model defined under class meta , exclude = ['captcha_text']
not work?
exclude = ['title']
exclude field form, not model. form.save()
try save model instance available fields, model might throw error pertaining missing field.
to add fields in model form, this:
class partialauthorform (modelform): extra_field = forms.integerfield() class meta: model = author def save(self, *args, **kwargs): # self.cleaned_data['extra_field'] super(partialauthorform, self).save(*args, **kwargs)
but make sure there no field called "partialauthorform" in model author.
Comments
Post a Comment