1

我正在尝试让 django-guardian 使用我的应用程序。我按照 django-guardian 文档中的安装和配置步骤进行操作。它无法使用“配置不当: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

设置.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
4

0 回答 0