【问题标题】:Django 1.3: Problems with UserProfile after upgradeDjango 1.3:升级后 UserProfile 出现问题
【发布时间】:2011-07-11 14:31:49
【问题描述】:

升级到 Django 1.3(从 1.2.3)后,以下行导致崩溃:

users = self.users.filter(userprofile__public_profile=True).order_by('first_name')

显示的错误:

Caught FieldError while rendering: Cannot resolve keyword 'userprofile' into field. Choices are: _message_set, comment, commentabusereport, date_joined, dialog, dialogabusereport, email, first_name, forums, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, registrationprofile, user_permissions, userassociation, username, vote

和以前一样,UserProfile 模型是这样指定的:

AUTH_PROFILE_MODULE = 'emailuser.UserProfile'

有趣的是,一些显示为可用的字段(例如“dialogabusereport”和“userassociation”)又是其他内部模型,它们与 UserProfile 中的用户关系类型相同。

有什么可能导致这种情况的想法吗?为什么 Django 在这个关系中看不到我们的 UserProfile 模型?

【问题讨论】:

  • 遇到了类似的问题,然后回到 Django 1.2.3,我的项目又可以工作了。希望我能帮助你回答 - 至少我能做到投票你的问题。
  • 您是否有机会在您的个人资料的应用程序 models.py 文件中从 django.contrib.auth.admin 导入 UserAdmin?可能是这个问题引起的:code.djangoproject.com/ticket/15771

标签: django django-1.3


【解决方案1】:

如果您尝试从用户对象访问配置文件模型不是正确的表示法:

user.get_profile()

UserProfile 模型应该是与 User 模型的反向 FK 关系,因此不能用作属性。

如果您想查找 userprofile = True 按 first_name 字段排序的所有 UserProfile 对象,则为:

userprofiles = UserProfile.objects.filter(public_profile=True).order_by('user__first_name')

【讨论】:

    【解决方案2】:

    事实证明,这是一个已知的 Django 错误,只有在您的代码中导入 UserAdmin 时才会出现。

    https://code.djangoproject.com/ticket/15771

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 2014-03-27
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2021-02-20
      相关资源
      最近更新 更多