Python Forum

Full Version: Django allauth saving custom user profile fields with signup form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am a beginner in Django. I am using Django 1.10 with an allauth app to create a user registration. There are couple of extra fields (phone_number and organization) which I have included in signup form apart from the first name, last name, email, password.

Creating a new user (forms.py)

from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteField
from phonenumber_field.formfields  import PhoneNumberField
from . import models

    class SignUpForm(forms.Form):
        first_name = forms.CharField(max_length=30)
        last_name = forms.CharField(max_length=30)
        phone_number = PhoneNumberField(label=_("Phone (Please state your country code eg. +91)"))
        organisation = forms.CharField(max_length=50)

        def signup(self, request, user):
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.save()
            user.profile.phone_number = self.cleaned_data['phone_number']
            user.profile.organisation = self.cleaned_data['organisation']
            user.profile.save()

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = models.UserProfile
        fields = ("company", "job_title", "website", "about_text", "location")

    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    location = AutoCompleteSelectField("city", required=False)
    job_title = AutoCompleteField("job_title")
    company = AutoCompleteField("company")
UserProfileForm works when the user is logged in and update the profile form(which contains job_title, company etc)

In models.py I have

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from easy_thumbnails.fields import ThumbnailerImageField
from ciasroot.settings import THUMBNAILER_SIZES, UPLOAD_PATH
from ciasroot.constants import GENDERS, LANGUAGES
from ciasroot.util import HashedPk
import math, decimal, datetime, os


class UserProfile(models.Model, HashedPk):
    user = models.OneToOneField(User, unique=True)
    company = models.CharField(max_length=128, blank=True, null=True)
    job_title = models.CharField(max_length=128, blank=True, null=False, default="")
    gender = models.CharField(max_length=1, choices=GENDERS, null=True, blank=True)
    website = models.URLField(max_length=255, blank=True, null=True)
In auth_user table the first_name,last_name,email,password is saving. The phone_number and organization are in another table which is general_userprofile. That table doesn't save phone_number and organization. I have tried inserting the data in that table manually and it worked.

In settings.py I put the below line

ACCOUNT_SIGNUP_FORM_CLASS = "general.forms.SignUpForm" 
Even if the general_userprofile table creates a row on signing up with correct user id, the field phone_number and organization is always empty.

Any help is highly appreciated.
It's all good now.

Added the following code in models.py

#Other imports
from phonenumber_field.modelfields import PhoneNumberField

class UserProfile(models.Model, HashedPk):
    user = models.OneToOneField(User, unique=True, related_name ='profile')
    organisation = models.CharField(max_length=50, blank=True, null=True)
    phone_number = PhoneNumberField( blank=True, null=True)
Changed the following code in forms.py

def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        """
        profile, created = models.UserProfile.objects.get_or_create(user=user)
        profile.phone_number = self.cleaned_data['phone_number']
        profile.organisation = self.cleaned_data['organisation']
        profile.save()
        user.save()
        """
        up = user.profile
        up.phone_number = self.cleaned_data['phone_number']
        up.organisation = self.cleaned_data['organisation']
        user.save()
        up.save()
The solution may not be the cleanest one but it works. If you have any other ways to do this then please let me know.