【问题标题】:Extending the User model with custom fields in Django在 Django 中使用自定义字段扩展用户模型
【发布时间】:2011-02-22 15:50:06
【问题描述】:

我正在尝试扩展用户模型,以便我可以添加自己的自定义字段,但我不断收到错误说明:

“NoneType”对象没有属性“_default_manager”

每当我尝试使用时

user.get_profile()

向自定义字段添加值,即每当我像这样使用它时:

user = User.objects.create_user(用户名、电子邮件、密码)

user.first_name = fname

user.last_name = lname

user.save()

uinfo = user.get_profile()

uinfo.timezone = "亚洲/浦那"

uinfo.save()

我已经按照在 Extending the User model with custom fields in Django 没有运气。

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    首先:您确定在 settings.py 文件中输入了正确的 AUTH_PROFILE_MODULE 值吗?因为如果没有此用户的 UserProfile,异常应该看起来不同。 (不存在)

    Anyway:当时没有 UserProfile 对象,因为它不是自动生成的。所以 get_profile() 在你的情况下会引发一个异常。

    要么这样做:

    if not UserProfile.objects.filter(user=user):
        p = UserProfile(user=user)
        p.save()
    uinfo = user.get_profile()
    ...
    

    或按照http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users 中(松散地)描述的那样创建一个信号

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 2012-05-01
      • 2014-02-23
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多