Django allauth使用注册表单保存自定义用户配置文件字段 [英] Django allauth saving custom user profile fields with signup form

查看:80
本文介绍了Django allauth使用注册表单保存自定义用户配置文件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的初学者.我将Django 1.10与allauth应用程序配合使用来创建用户注册.除了名字,姓氏,电子邮件和密码外,注册表单中还包括几个其他字段(电话号码和组织).

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.

创建新用户(forms.py)

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在用户登录并更新个人资料表单(包含job_title,公司等)时起作用

UserProfileForm works when the user is logged in and update the profile form(which contains job_title, company etc)

在models.py中,我有

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)

auth_user 表中,名字,姓氏,电子邮件,密码正在保存.电话号码和组织位于另一个表中,该表为 general_userprofile .该表不保存phone_number和组织.我尝试过手动将数据插入该表中,并且可以正常工作.

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.

在settings.py中,我在下面一行

In settings.py I put the below line

ACCOUNT_SIGNUP_FORM_CLASS = "general.forms.SignUpForm"

即使general_userprofile表在使用正确的用户ID进行注册时创建了一行,phone_number和organization字段也始终为空.

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.

推荐答案

在模型中 UserProfile

user = models.OneToOneField(User, unique=True)

这意味着,您需要访问诸如 request.user.userprofile 之类的配置文件,因此您可以在模型字段中添加 related_name 并进行迁移,

this means, you need to access the profile like request.user.userprofile, so either you can add a related_name in your model field and migrate,

user = models.OneToOneField(User, unique=True related_name='profile')

或者您可能需要在表单中将 profile 更改为 userprofile

Or you may need to change profile to userprofile in your form,

无论哪种方式,我想如果 user 尚未创建 UserProfile ,那么您可能需要更改表格,

Either way, I suppose if the user has no UserProfile yet created then, you may need to change your form,

def signup(self, request, user):
    user.first_name = self.cleaned_data['first_name']
    user.last_name = self.cleaned_data['last_name']
    user.save()
    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()

这篇关于Django allauth使用注册表单保存自定义用户配置文件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆