Reputation: 429
I have been pulling my hair out for a couple of days trying to get additional fields such as first name and last name to save with Django Registration. I am getting the form fields to show up but after I submit the form, the additional fields don't save. Please help me. Thank you!
Models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
Forms.py
from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from MyRegistration.models import UserProfile
from django.contrib.auth.models import User
class UserRegistrationForm(RegistrationForm):
first_name = forms.CharField(label=_('First Name'), max_length=30, required=True)
last_name = forms.CharField(label=_('Last Name'), max_length=30)
Regbackend.py
from django.contrib.auth.models import User
from MyRegistration.forms import UserRegistrationForm
from MyRegistration.models import UserProfile
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
data = Profile(user=user)
data.first_name = form.data['first_name']
data.last_name = form.data['last_name']
data.save()
from registration.signals import user_registered
user_registered.connect(user_created, sender = UserProfile)
Urls(root).py
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
from MyRegistration.forms import UserRegistrationForm
from registration.views import register
import registration.backends.default.urls as regUrls
import MyRegistration.regbackend
urlpatterns = patterns('',
url(r'^accounts/register/$', register, {'backend':
'registration.backends.default.DefaultBackend','form_class': UserRegistrationForm},
name='registration_register'),
(r'^accounts/', include(regUrls)),
Upvotes: 2
Views: 1125
Reputation: 1
Nice. This works okay. I did it this way:
def register(self, request, **kwargs):
......
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
# This is my additional block
new_user.first_name = kwargs['first_name']
new_user.last_name = kwargs['last_name']
new_user.save()
data = UserProfile(user=new_user)
data.gender = kwargs['gender']
data.address1 = kwargs['address1']
data.address2 = kwargs['address2']
data.city = kwargs['city']
data.state = kwargs['state']
data.pincode = kwargs['pincode']
data.save()
# end of my block. The line below, which didn't work, was
# actually replaced by the above code. Ugly but works
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
Upvotes: 0
Reputation: 12018
I had troubles trying to save extra stuff using the signal.
What I do is get the DefaultBackend of django-registration, copy it into my project, and add there the code to save the extra fields.
Then in urls.py I tell "^accounts/register/$" to use my personalized backend.
Upvotes: 1