Django - 创建用户配置文件 [英] Django - Create user profile on user creation

查看:162
本文介绍了Django - 创建用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Django文档 here ,以达到一个简单的目标:创建一个新的用户创建一个用户配置文件。

I'm following Django documentation here in order to achieve a simple objective: Create a user profile as soon as a new user is created.

我有一个帐户应用程序和我的帐户.models看起来像这样:

I have an 'accounts' app and my accounts.models looks like this:

# -*- 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管理员)时,而不是拥有新创建的用户和用户配置文件,我得到以下e rror:
/ admin / auth / user / add /
中的InternalError当前事务被中止,忽略直到事务块结束的命令

Everything looks fine to me but when trying to add a new user (using django admin), instead of having a newly created user and user profile, I get the following error: InternalError at /admin/auth/user/add/ current transaction is aborted, commands ignored until end of transaction block

这是追溯错误部分:

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

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

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

It seems like an integrity error but I'm not getting the reason for it.

如果有任何可以给我一些帮助,这将是伟大的。 p>

Would be great if any of ya could give me some help on this.

推荐答案

只是想出来了。

我忘了添加'null =现在,UserProfile模型栏的其余部分的True'字段选项=)

I forgot to add 'null=True' field option to the rest of UserProfile model fields =)

所以accounts.models UserProfile字段现在看起来像:

So the accounts.models UserProfile fields now looks like:

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)

...一切都按照预期工作!

...and everything is working as intended!

欢呼试图帮助Ashray ^^

Cheers for trying to help Ashray ^^

这篇关于Django - 创建用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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