我正在尝试让django-guardian使用我的应用程序。我按照django-guardian文档中的安装和配置步骤进行操作。它无法通过“NotperlyConfigured:AUTH_USER_MODEL引用尚未安装的模型'auth.User'来分配权限。”
我已经阅读了相关的Django文档,并了解AUTH_USER_MODEL设置在使用自定义用户模型时使用“app_label.UserModel”格式。我没有使用自定义用户模型,而是使用默认的django模型。
我在互联网中发现的所有问题和解决方案都与我认为不适用于我的自定义用户模型有关。
我正在使用django 1.6.1和django-guardian 1.1.1。 dj-guardaian教程:
http://django-guardian.readthedocs.org/en/latest/userguide/assign.html
我尝试设置settings.AUTH_USER_MODEL ='guardian.User'无济于事。
任何帮助表示赞赏!
干杯
这些是确切的步骤(导入设置后):
>>> from django.contrib.auth.models import User
>>> john = User.objects.get(id=2)
>>> john
<User: john>
>>> from mtm.models import Sharing
>>> obj = Sharing.objects.get(id=1)
>>> obj
<Sharing: Sharing object>
>>> john.has_perm('mtm.change_sharing', obj)
False
>>> from guardian.shortcuts import assign_perm
>>> assign_perm('mtm.change_sharing', john, obj)
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
assign_perm('mtm.change_sharing', john, obj)
File "/usr/local/lib/python2.7/dist-packages/guardian/shortcuts.py", line 71, in assign_perm
user, group = get_identity(user_or_group)
File "/usr/local/lib/python2.7/dist-packages/guardian/utils.py", line 73, in get_identity
if isinstance(identity, get_user_model()):
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py", line 129, 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 'auth.User' that has not been installed
settings.py
import os
import djcelery
djcelery.setup_loader()
CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend'
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY =
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ss',
'debug_toolbar',
'djcelery',
'mtm',
'guardian',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'p4.urls'
WSGI_APPLICATION = 'p4.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = False
USE_TZ = True
#changed the default display formats
DATETIME_FORMAT = 'Y-m-d H:i:s'
DATE_FORMAT = 'Y-m-d'
TIME_FORMAT = 'H:i:s'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
MEDIA_ROOT = '/home/kp/workspace/p4/'
MEDIA_URL = 'http://localhost:8000/media/'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
)
ANONYMOUS_USER_ID = -1