内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/11488974/django-create-user-profile-on-user-creation
,以实现一个简单的目标:创建一个新的用户创建一个用户配置文件.
我有一个’帐户’应用程序和我的帐户.模型看起来像这样:
# -*- 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管理员),而不是拥有新创建的用户和用户配置文件时,我会收到以下错误:
/ admin / auth / user / add /
当前事务被中止,命令被忽略,直到事务块结束
这是追溯错误部分:
/djangoProjects/lwboanova/lwboanova/apps/accounts/models.py in create_user_profile 34: UserProfile.objects.create(user=instance)
这似乎是一个完整性错误,但我没有得到它的理由.
如果有任何一个可以给我一些帮助,这将是伟大的.
只是想出来
我忘了在其余的UserProfile模型字段中添加’null = True’字段选项=)
所以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 ^^
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/11488974/django-create-user-profile-on-user-creation
以上所述就是小编给大家介绍的《Django – 创建用户配置文件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- iOS App创建证书,添加Appid,创建配置文件流程
- 用命令创建用户、配置 IIS、配置 SQL Server
- Express教程01:创建服务器、配置路由
- Spring 异步任务的创建、自定义配置和原理
- Create React App 2.1.2 发布,零配置创建 App
- Create React App 3.0.1 发布,零配置创建 App
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First HTML and CSS
Elisabeth Robson、Eric Freeman / O'Reilly Media / 2012-9-8 / USD 39.99
Tired of reading HTML books that only make sense after you're an expert? Then it's about time you picked up Head First HTML and really learned HTML. You want to learn HTML so you can finally create th......一起来看看 《Head First HTML and CSS》 这本书的介绍吧!
JSON 在线解析
在线 JSON 格式化工具
图片转BASE64编码
在线图片转Base64编码工具