扩展 Django 用户模型
我正在 django 中扩展 User 对象并拥有用户配置文件。因此,使用 post_save 钩子,我通过以下方式保存用户配置文件:
class UserProfile(models.Model):
user = models.OneToOneField(User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
userObj, createdUser = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
当我通过 python manage.py shell 运行 shell 时,它工作正常;在表中保存用户对象和用户配置文件,但如果我通过启动请求进行测试,在处理该请求的views.py中,我会执行以下操作
def handleRequest(request):
user = User(username="test",email="test",password="test")
user.save()
当然,我创建了一个 HttpResponse 并返回了一个响应,但为了简洁而省略了它。结果是我得到一个保存在 auth_user 中的用户条目,但没有用户配置文件。什么事?请帮忙。
I am extending the User object in django and have a user profile. So using the post_save hook I save a user profile in the following way:
class UserProfile(models.Model):
user = models.OneToOneField(User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
userObj, createdUser = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
It works fine when I run the shell, via python manage.py shell; saving a user object and user profile in the tables but if I test by launching a request, in the views.py that handles that request I do the following
def handleRequest(request):
user = User(username="test",email="test",password="test")
user.save()
Of course I created a HttpResponse and returned a response as well but omitted it for brevity. The results are i get a User entry saved in auth_user but no user profile. What the dealy? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法访问用户配置文件,因为您在用户配置文件中已经存在关系。
你需要添加字段。
有关更多信息,请检查以下链接
http ://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
can not access the user profile, because you have in the user profile is a relationship.
you need add fields.
for more information check the following link
http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/