Updating First and Last Name During Registration - Django CMS

Go To StackoverFlow.com

0

I'm currently working on an application for user registration on a Django CMS site.

My registration form is working just fine and creates the user, plus adds my custom fields to my registration model.

What I'd like to know is how to update auth_user to contain the user's first and last name.

Currently the user is being created in this manner:

import newform
from betaregistration.newform import RegistrationFormZ

def user_created(sender, user, request, **kwargs):
    form = RegistrationFormZ(request.POST)
    data = newform.BetaProfile(user=user)
    data.first_name = form.data["first_name"]
    data.last_name = form.data["last_name"]
    data.address = form.data["address"]
    data.city = form.data["city"]
    data.state = form.data["state"]
    data.postal_code = form.data["postal_code"]
    data.country = form.data["country"]
    data.phone= form.data["phone"]
    data.email = form.data["email"]
    data.save()

    # this was the solution
    user.first_name = form_data['first_name']
    user.last_name = form_data['last_name']
    user.save()
    # end solution

from registration.signals import user_registered
user_registered.connect(user_created)

Any help is appreciated. I'm using django-registration for my registration.

2012-04-04 19:48
by plumwd


1

Since you are passing user into user_created could you simply modify things as the following:

data.first_name = user.first_name = form.data["first_name"]
data.last_name = user.last_name = form.data["last_name"]

....

user.save()

I may be missing something but if you really do have access to user in user_created that should work. You could also potentially do the same using request.user instead of user.

2012-04-04 20:52
by joshcartme
Seemed wonderfully simple, but didn't save the info in auth_user - plumwd 2012-04-04 23:52
Hmm, could you try logging the value of user at the beginning of user_created to ensure that it actually is something? You may need to do something like user=kwargs['user'] rather than putting it in the method declaration - joshcartme 2012-04-05 16:32
Your comment got me on the right track, I edited my original question to show how I fixed it - plumwd 2012-04-06 00:41
Glad you figured it out! You should be able to shorten it even further as I had it in this answer as long as you call user.save() (I updated my answer to reflect this). I think I had missed before that you weren't calling user.save() which was the problem - joshcartme 2012-04-06 01:22
I may try it your way just to see how that works - plumwd 2012-04-07 04:30
Ads