简体   繁体   English

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

[英]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_usergeneral_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.

相关问题 如何保存在 django 管理站点中创建和修改记录的用户? - How to save user that created and modified a record in django admin site? Django admin 仅为登录用户保存和显示数据 - Django admin save and display data only for the logged in user 在管理员中创建的 Django CustomUser,通过电子邮件通知用户 - Django CustomUser created in Admin, User notification by email Django Model 管理员:如何使用当前用户自动分配和隐藏 created_by 字段并在保存后显示? - Django Model Admin: How to auto assign and hide created_by field with current user and display after save? 在Django中保存用户特定的数据 - Save user specific data in Django 在 django 如何自定义 model 管理员以同时保存在 auth_user 和另一个不相关的 model 中? - In django how to customize model admin to save in auth_user and another unrelated model simultaneously? 尝试将禁止的用户添加到表Django ADMIN中 - Trying to add banned user into table, Django ADMIN 一旦用户在 django 中注册,如何自动将用户表链接到另一个表 - How to automatically link the users table to another table once a user gets registered in django 将用户数据传递给 Django 中的管理员操作 - Passing User Data to Admin Actions in Django 用户数据未在管理面板和 forms django 中更新 - User data not updating in admin panel and forms django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM