Allauth user fields and userProfile fields save in one registrationform

115 views
Skip to first unread message

Marco Neumann

unread,
Jul 7, 2015, 1:00:41 PM7/7/15
to django-d...@googlegroups.com
Hi All

Can someone point me in the right direction.
 
On registration I would like to fill in the UserFields(Username-password etc) and the ProfileFields (City, phonenumber etc)  and save theme both in the models User and UserProfile

Need i to expaned the sigupform or put two form on one page and then save? (Hoe is this working?)

has somebody a example i can look at?

Thanks in advanced.

Marco Neumann

Curtis Maloney

unread,
Jul 8, 2015, 8:58:47 AM7/8/15
to django-d...@googlegroups.com
This is a list for discussion of the development _of_ Django, not development _with_ Django.

This question is better suited to the django-users list.

In general, if you need to edit two models in one form submission, you use two ModelForms... I have a blog post on this topic: http://musings.tinbrain.net/blog/2015/jun/10/updating-two-models-single-post/

--
Curtis


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/f653afbf-dcac-4ffa-b716-cab7d02e61fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sadaf Noor

unread,
Jul 8, 2015, 6:42:42 PM7/8/15
to django-d...@googlegroups.com
Usually you would have separated ModelForm child for your User and Profiles:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())
    class Meta:
        model = User
        fields = ('username', 'email', 'password')
class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('website', 'picture')

At the the time of saving you are just passing the data to initialize your ModelForms.

user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)


To show form you need to pass these forms to template:
user_form = UserForm()
profile_form = UserProfileForm()


To show at template just call as_p of your ModelForm:
 {{ user_form.as_p }}
 {{ profile_form.as_p }}

I think this is the easiest way to do so....


For more options, visit https://groups.google.com/d/optout.



--
 Md. Sadaf Noor (@sadaf2605)
 www.sadafnoor.com
Reply all
Reply to author
Forward
0 new messages