Django的 - 创建用户创建用户资料(Django - Create user profile

2019-06-26 06:53发布

我下面的Django文档在这里 ,以实现一个简单的目标:为创建一个新用户尽快建立用户档案。

我有一个“账户”,应用程序和我accounts.models看起来是这样的:

# -*- coding: utf-8 -*-
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from main.models import Store

class UserProfile(models.Model):

    GENRE_CHOICES = (
        ('m', 'Masculino'),
        ('f', 'Feminino'),
    )
    MARITAL_STATUS_CHOICES = (
        ('s', 'Solteiro'),
        ('c', 'Casado'),
        ('d', 'Divorciado'),
        ('v', 'Viúvo'),
    )

    user = models.ForeignKey(User, unique=True)
    birth_date = models.DateField()
    genre = models.CharField(max_length=1, choices=GENRE_CHOICES)
    address = models.CharField(max_length=150)
    postal_code_4 = models.PositiveIntegerField()
    postal_code_3 = models.PositiveIntegerField()
    locatity = models.CharField(max_length=30)
    marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES)
    child_amount = models.PositiveSmallIntegerField()
    is_merchant = models.BooleanField(default=False)
    store = models.ForeignKey(Store, null=True)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

一切看起来没什么问题,但尝试添加新用户时(使用Django管理),而不是有新创建的用户和用户配置文件,我得到以下错误: 在/管理/认证/用户InternalError该/添加/当前交易忽略,直到事务块的端中止,命令

这里是追踪误差部分:

/djangoProjects/lwboanova/lwboanova/apps/accounts/models.py in create_user_profile

34: UserProfile.objects.create(user=instance)

这似乎是一个完整的错误,但我没有得到它的原因。

将是巨大的,如果任何雅的可以给我一些这方面的帮助。

Answer 1:

只要理解了它。

我忘了补充null=True到其余UserProfile模型字段。

所以accounts.models.UserProfile领域现在看起来像:

user = models.ForeignKey(User, unique=True)
birth_date = models.DateField(null=True)
genre = models.CharField(max_length=1, choices=GENRE_CHOICES, null=True)
address = models.CharField(max_length=150, null=True)
postal_code_4 = models.PositiveIntegerField(null=True)
postal_code_3 = models.PositiveIntegerField(null=True)
locatity = models.CharField(max_length=30, null=True)
marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES, null=True)
child_amount = models.PositiveSmallIntegerField(null=True)
is_merchant = models.BooleanField(default=False)
store = models.ForeignKey(Store, null=True)

......,一切都如预期运行!

干杯试图帮助Ashray ^^



Answer 2:

你不应该使用:

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

相反,使用此:

from django.conf import settings
..
user = models.OneToOneField(settings.AUTH_USER_MODEL)


Answer 3:

def create_profile(sender,**kwargs ):
    if kwargs['created']:
        user_profile=UserProfile.objects.create(user=kwargs['instance'])


post_save.connect(create_profile,sender=User)

我认为这会帮助你。



Answer 4:

    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if kwargs.get('created', True) and not kwargs.get('raw', False):
            Profile.objects.create(user=instance)


文章来源: Django - Create user profile on user creation