Validate nested form in Django -
i have userprofile
model linked onetoone django user
model. while adding user through form, wanted have 1 form both of those. found snippet that, , didn't have problem it, until wanted validate username
. form isn't throwing validationerror
, like to, returns error page valueerror: user not created because data didn't validate.
.
from understand (which wrong), current setup doesn't handle validationerror
s nested form.
is there way add functionality? if not, how should tackle issue of having 1 form handle models?
code:
class cmuserform(forms.modelform): password = forms.charfield(widget=forms.passwordinput(), required=false, help_text=_("leave empty if don't want " "to change it")) def clean_password(self): data = self.cleaned_data['password'] if data.strip(): return make_password(data) else: # if password field empty, don't change return self.instance.password def clean_username(self): username = self.cleaned_data['username'] if get_user_model().objects.filter(username=username).exists(): raise forms.validationerror(_('this username in use.')) return username class meta: model = get_user_model() fields = ('first_name', 'last_name', 'email', 'username', 'password', 'is_staff', 'is_active') class cmuserprofileform(forms.modelform): def __init__(self, *args, **kwargs): # take user if updating, none if creating try: self.user = kwargs['instance'].user except attributeerror: self.user = none user_kwargs = kwargs.copy() user_kwargs['instance'] = self.user self.uf = cmuserform(*args, **user_kwargs) super(cmuserprofileform, self).__init__(*args, **kwargs) self.fields.update(self.uf.fields) self.initial.update(self.uf.initial) class meta: model = userprofile exclude = ['user'] def save(self, *args, **kwargs): # save user , pass userprofile user = self.uf.save(*args, **kwargs) self.instance.user = user return super().save(*args, **kwargs)
i implemented same today. learnt website tango django. http://www.tangowithdjango.com/book/chapters/login.html. provided code how achieved this. inbuilt user model checks if username exists or not. hope helpful.
class sam(models.model): user = model.onetoonefield(user) #custom fields apart inbuilt user model region = models.charfield(max_length=10) designation = models.charfield(max_length=10) #forms.py form models. created samprofileform capture custom fields specific one's profile , samform capture password , hash later in view. #please note didnt add username check here. inbuilt user us. verified it. class samform(forms.modelform): #form model capture inbuilt fields of "user" model password = forms.charfield(widget=passwordinput()) class meta: model = user fields = ('username', 'email', 'password', 'firstname', 'lastname') class samprofileform(forms.modelform): #form model built custom fields in case region , designation class meta: model = sam fields = ('desgination', 'mgr') def register(request): registered = false if request.method == 'post': user_form = samform(data=request.post) profile_form = samprofileform(request.post) if user_form.is_valid() , profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=false) profile.user = user profile.save() registered = true else: print user_form.errors, profile_form.errors else: user_form = samform() profile_form = samprofileform() template = loader.get_template('sam/register.html') context = requestcontext(request, { 'user_form' : user_form, 'profile_form' : profile_form, 'registered' : registered, }) return httpresponse(template.render(context))
Comments
Post a Comment