[英]Save user data in another table once a user is created in django admin
I am a beginner in django. 我是django的初学者。 I am using django 1.10 with allauth
app which takes care of frontend registration, sending user confirmation email,validation, login with email etc. 我正在使用django 1.10和allauth
应用程序,它负责前端注册,发送用户确认电子邮件,验证,使用电子邮件登录等。
The allauth app inserts three rows in three tables when a user is created. 在创建用户时,allauth应用程序在三个表中插入三行。
1. auth_user
2. account_emailaddress
3. account_emailconfirmation
When adding a user from admin it creates a row in auth_user
and general_userprofile
table. 从admin添加用户时,它会在auth_user
和general_userprofile
表中创建一行。 I would like to insert a row in account_emailaddress
table when admin creates a user. 我想在admin创建用户时在account_emailaddress
表中插入一行。
Fields in account_emailaddress are-- account_emailaddress中的字段是 -
id
email
verified
primary
user_id
The models.py looks like -- 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 look like this 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()
How can I get the user id and email which is created under class EmailAddress
and save it to the table. 如何获取在EmailAddress
类下创建的用户ID和电子邮件,并将其保存到表中。
First of all if you want to store user id and email in your EmailAdress table you must had these fields in your model. 首先,如果要在EmailAdress表中存储用户ID和电子邮件,则必须在模型中包含这些字段。
After this, when your handler receives the user creation signal, you can add something like this in it. 在此之后,当您的处理程序收到用户创建信号时,您可以在其中添加这样的内容。
@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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.