“用户”对象没有属性“UserProfile”

2024-04-05

我尝试按照以下示例进行操作https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model关于如何扩展用户模型。但我无法检索附加到用户的模型数据。

model

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    ip = models.IntegerField(default=0)

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

post_save.connect(create_user_profile, sender=User)

获取我的 User 对象

user = auth.authenticate(username=username, password=password)
g = user.UserProfile.ip
print(g)

用户被正确获取,并且我能够获取标准用户的所有数据。但 user.UserProfile 会导致:

Internal Server Error: /Crowd/login/
Traceback (most recent call last):
  File "C:\Anaconda3\Lib\site-packages\django\core\handlers\base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Anaconda3\Lib\site-packages\django\core\handlers\base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Rasmus\workspace\Crowd\src\Cr\views.py", line 35, in login
    g = user.UserProfile.ip
AttributeError: 'User' object has no attribute 'UserProfile'
[05/Sep/2016 04:32:46] "POST /Crowd/login/ HTTP/1.1" 500 69185

我检查了我的数据库,可以看到 UserProfile 中有一行与 User 有关系。所以 UserProfile 已正确创建(我假设)。


您应该访问以下用户的个人资料user with user.userprofile, not user.UserProfile。尝试浏览一下一对一关系的文档 https://docs.djangoproject.com/en/1.10/topics/db/examples/one_to_one/。它应该使 Django 希望您如何使用此类关系变得更加清晰。

另请注意,从UserProfile侧,您可以访问User with .user not .User。您将看到,这是 Django 中的关系始终使用的命名约定。同样,默认情况下,数据库中的表将被命名为类似myapp_userprofile而不是myapp_UserProfile.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“用户”对象没有属性“UserProfile” 的相关文章

  • Python用Django请求Post请求数据

    我正在尝试向一个非常简单的 django 服务器发送一个简单的 post 请求 但无法理解为什么 post 数据没有出现在 requests post 字典中 而是出现在请求正文中 客户端代码 payload test test heade
  • django:如何在模板html页面内进行计算?

    您好 我正在使用缩略图插件来获取图像的宽度和高度 现在我想使用从缩略图插件获取的高度来定义 img 标签的填充 例如 img style padding top img height 2 src 但是我在这里遇到错误 django不允许这样
  • 具有 auto_now_add 的 Django DateTimeField 要求默认值

    我的模型中有这个字段created at models DateTimeField auto now add True 当我尝试进行迁移时出现错误 You are trying to add the field created at wit
  • 在 Django shell 会话期间获取 SQL 查询计数

    有没有办法打印 Django ORM 在 Django shell 会话期间执行的原始 SQL 查询的数量 Django 调试工具栏已经提供了此类信息 例如 5 QUERIES in 5 83MS但如何从 shell 中获取它并不明显 您可
  • 没有 Neo4Django 的 Django 和 Neo4j

    我正在使用 Neo4j 以及 Postgres 构建一个 Django 应用程序 我发现这个 Django 集成称为新4django https github com scholrly neo4django 我想知道是否可以只使用neo4r
  • 在 Django 中保存模型会出现“警告:字段‘id’没有默认值”

    我在 Django 中有一个非常基本的模型 class Case models Model name models CharField max length 255 created at models DateTimeField defau
  • Django 营业时间

    我想添加诊所的营业时间 我已经对此进行了调查在 Django 中实现 开放时间 的任何现有解决方案 https stackoverflow com questions 8128143 any existing solution to imp
  • 在 ubuntu 中卸载 python 模块

    我必须删除一个名为 django 的 python 模块 一种流行的模块 因为我安装了错误的版本 1 3 py 2 6 中的 beta 如何卸载这个模块 请解释一下 因为我只在 Windows 中使用过 python 而从未在 Ubuntu
  • 如何去除html源代码中的空格

    我正在使用 django 和 python 在模板文件中 我有一个下拉列表 如下所示 有用 唯一的问题是源 html 代码之间有很多空白 有什么办法可以去除空白吗 谢谢 for lang ele in video languages all
  • python:日志记录:我们可以向记录器添加多个过滤器吗?考虑哪一个

    我试图了解 Python 日志记录中的多个过滤器 一个在配置中定义 另一个在代码中定义 如何工作 我正在开发一个 Django 项目 下面是我在 settings py 中的记录器配置 我的目标是switch on and switch o
  • Django - 简单的搜索表单

    使用 Django 1 9 和 Python 3 5 我想制作一个简单的搜索表单 views py from django views import generic from django shortcuts import render f
  • 如何获取Django当前的语言?

    如何获取 Django 中的当前语言 特别感兴趣的功能是django utils translation get language 它返回当前线程中使用的语言 看文档 https docs djangoproject com en dev
  • 在 Django Rest Framework 中访问视图集和序列化器中的请求对象?

    我想访问 DRF 中 Views py 和 Serializers py 中的请求对象 我的观点 py class ProductViewSet viewsets ReadOnlyModelViewSet This viewset auto
  • 如何限制Django CreateView中ForeignKey字段的选择?

    我有一个沿着这些思路的模型结构 models py class Foo models Model class Bar models Model foo models ForeignKey Foo class Baz models Model
  • Django 在选择列表更改时创建毫无意义的迁移

    我正在尝试使用可调用创建一个带有选择字段的模型 以便 Django 在选择列表更改时不会创建迁移 如中所述this https stackoverflow com questions 31788450 stop django from cr
  • Django South - 将 null=True 字段转换为 null=False 字段

    我的问题是 转变的最佳做法是什么null True场变成null False使用 Django South 的字段 具体来说 我正在与ForeignKey 你应该先写一个数据迁移 http south aeracode org docs t
  • Django Widget Media 不起作用

    我需要一个小部件 它应该只显示一个块 因为我将使用 jQuery 添加功能 我试图通过 Widget 的 Media 类包含 javascript 和样式表 但它对我不起作用 这是代码 class StarsWidget Widget Wi
  • iFrame 在 Facebook Canvas 应用程序中显示为空白

    我有一个非常简单的页面 我试图在 Facebook iframe 中查看它 它是一个 Django 视图 但它不依赖于请求是通过 POST 还是 GET 提交 它所做的只是返回一些简单的 HTML 如果我们直接点击链接 它会正确显示 如果在
  • Django 本身支持迁移吗

    我听说所有新的 Web 框架 如 ROR Django 等 都遵循敏捷和 TDD 的一般原则 遵循敏捷和 TDD 的一部分是在从一个迭代到另一个迭代时制定自己的设计 这意味着模型及其架构将随着应用程序的不同版本而演变 我知道 ROR 本身支
  • 无法使用数据库路由器通过 save_model 进行保存

    我正在使用数据库路由器 因此我的应用程序有两个数据库 一个数据库用于默认 django 数据 另一个数据库用于存储默认 django 数据 在我的管理员中我已经覆盖了save model函数以保存created by变量 但我无法做到这一点

随机推荐