Django 1.5:访问models.py中的自定义用户模型字段 [英] Django 1.5: Accessing custom user model fields in models.py

查看:146
本文介绍了Django 1.5:访问models.py中的自定义用户模型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Django 1.5项目,我有一个自定义的用户模型(我们称之为 CustomUser )。另一个应用程序(SomeApp)需要引用此自定义用户模型。为了ForeignKey等的目的,Django文档说要使用

I'm working on a Django 1.5 project and I have a custom user model (let's call it CustomUser). Another app (SomeApp) needs to reference this custom user model. For the purposes of ForeignKey and such, the Django documentation says to use

User = settings.AUTH_USER_MODEL 

/ code>。但是用户现在是一个字符串而不是一个类,所以 User.objects 失败。替代方案是从django.contrib.auth导入get_user_model
User = get_user_model()


However, some functions in SomeApp.models need to access what would have formerly been known as User.objects. But User is now a string and not a class, so User.objects fails. The alternative would be

from django.contrib.auth import get_user_model
User = get_user_model()

其他模块可以工作,但是当我在SomeApp的models.py中使用它时,Django引发了一个错误:

Which works in other modules, but when I use this in models.py of SomeApp, Django raises an error:


不正确的配置(AUTH_USER_MODEL是指尚未安装的%s型%settings.AUTH_USER_MODEL)

ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)

任何想法?

编辑1 - 追溯:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "...\django-badger\badger\__init__.py", line 7, in <module>
    from badger.models import Badge, Award, Progress
  File "...\django-badger\badger\models.py", line 26, in <module>
    User = get_user_model()
  File "...\lib\site-packages\django\contrib\auth\__init__.py", line 127, in get_user_model
    raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
ImproperlyConfigured: AUTH_USER_MODEL refers to model 'MyApp.AuthUser' that has not been installed

编辑2 - INSTALLED_APPS设置:

EDIT 2 - INSTALLED_APPS settings:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'south',
'MyApp',   # this is where my user model is defined
'SomeApp', # I try to use get_user_model() in this app's models.py; doesn't work.
'social_auth',
)


推荐答案

我有这么多的递归包含问题等等...
嗯,最简单的事情,当你添加一个 ForeignKey ,是写它像这样:

Easy one, I think. I have had so many problems with recursive inclusions and so on... Well, the simplest thing to do, when you add a ForeignKey, is to write it like so:

user = models.ForeignKey(settings.AUTH_USER_MODEL, null=False, on_delete=models.CASCADE, verbose_name=_(u"User"))

如果您使用 get_user_model ,不要像你这样使用它。调用

If you use get_user_model, do not use it like you do. Calling

User = get_user_model()

$ b模块顶部的
$ b

将尝试导入您的用户模型,这可能确实没有被安装。
相反,您有几种选择:

at the top of the module will try to import your User model, which may, indeed, not have been "installed". Instead, you have several choices:


  • 在您的模块顶部,写入

  • At the top of your module, write

User = get_user_model#然后,您将不得不使用User()而不是用户

User = get_user_model # then, you will have to use User() instead of User

写入 get_user_model()无处不在。始终在方法或功能中,从不直接在模型模块正文中。

Write get_user_model() everywhere it's useful. Always in methods or functions, never directly in a model module body.

这篇关于Django 1.5:访问models.py中的自定义用户模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆