Python Forum
Django allauth saving custom user profile fields with signup form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django allauth saving custom user profile fields with signup form
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to profile tornado web app ? umen 2 969 Oct-09-2023, 05:59 PM
Last Post: noisefloor
  Selenium fields containing a word graham23s 3 2,841 Aug-22-2023, 12:35 PM
Last Post: Gaurav_Kumar
  Registration Form Fields rwahdan 2 1,897 Aug-09-2022, 10:35 AM
Last Post: Addweb
  Knowing User's Connection Type in Django Dexty 2 1,587 Apr-27-2022, 11:54 AM
Last Post: Dexty
  register the user as staff member - django rwahdan 0 1,485 Dec-24-2021, 03:08 PM
Last Post: rwahdan
  Error - ManyToMany Fields in Django rob25111 0 1,523 Jan-17-2021, 04:58 PM
Last Post: rob25111
  trying to display custom error message on form mp3909 0 1,577 Nov-19-2020, 06:35 PM
Last Post: mp3909
  Django send email - email form Remek953 2 2,295 Sep-18-2020, 07:07 AM
Last Post: Remek953
  How to send notifications to gmail from contact form using Django and pushbullet Justchse 0 1,868 Sep-01-2020, 01:19 PM
Last Post: Justchse
  Django admin form with parent child tmmaersk 0 1,863 Apr-02-2020, 06:42 AM
Last Post: tmmaersk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020