gpt4 book ai didi

python - 在 django admin 中创建用户后,将用户数据保存在另一个表中

转载 作者:行者123 更新时间:2023-11-28 17:13:39 24 4
gpt4 key购买 nike

我是 django 的初学者。我将 django 1.10 与 allauth 应用程序一起使用,它负责前端注册、发送用户确认电子邮件、验证、使用电子邮件登录等。

创建用户时,allauth 应用程序会在三个表中插入三行。

1. auth_user
2. account_emailaddress
3. account_emailconfirmation

当从管理员添加用户时,它会在 auth_usergeneral_userprofile 表中创建一行。当管理员创建用户时,我想在 account_emailaddress 表中插入一行。

account_emailaddress 中的字段是--

id
email
verified
primary
user_id

models.py 看起来像 --

class EmailAddress(models.Model):
#user = models.ForeignKey(User)
user = models.OneToOneField(User, unique=True, related_name ='address')
email = models.EmailField()
verified = models.BooleanField(verbose_name=_('verified'), default=True)
primary = models.BooleanField(verbose_name=_('primary'), default=True)

class Meta:
db_table = 'account_emailaddress'


class UserProfile(models.Model, HashedPk):
user = models.OneToOneField(User, unique=True, related_name ='profile')
job_title = models.CharField(max_length=128, blank=True, null=False, default="")
website = models.URLField(max_length=255, blank=True, null=True)
organisation = models.CharField(max_length=50, blank=True, null=True, default="")
phone_number = PhoneNumberField( blank=True, null=True)


@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
EmailAddress.objects.create(user=instance

)

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
from captcha.fields import ReCaptchaField


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. +44)"))
organisation = forms.CharField(max_length=50)
email = forms.EmailField()
password1 = forms.CharField(max_length=20)
password2 = forms.CharField(max_length=20)
captcha = ReCaptchaField(attrs={'theme' : 'clean'})


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()

如何获取在 EmailAddress 类下创建的用户 ID 和电子邮件并将其保存到表中。

最佳答案

首先,如果您想在 EmailAdress 表中存储用户 ID 和电子邮件,您的模型中必须有这些字段。

在此之后,当您的处理程序收到用户创建信号时,您可以在其中添加类似的内容。

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
EmailAdress.objects.create(user_id = instance.id, user_email=instance.email)

关于python - 在 django admin 中创建用户后,将用户数据保存在另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45642350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com