[英]Python-Social-Auth fails with mongoEngine (Django)
I try to make python-social-auth
work with mongodb
. 我尝试使
python-social-auth
与mongodb
。
I follow the instructions here that say to add: 我按照此处的说明添加:
INSTALLED_APPS = (
...
'social.apps.django_app.me',
...
)
and 和
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.me.models.DjangoStorage'
However something goes wrong and I get an ImportError: 但是出了点问题,我得到一个ImportError:
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x101c51d50>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 92, in inner_run
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 280, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 166, in get_app_errors
self._populate()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
self.load_app(app_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 96, in load_app
models = import_module('.models', app_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/social/apps/django_app/me/models.py", line 29, in <module>
'mongoengine.django.auth.User'
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/social/utils.py", line 21, in module_member
module = import_module(mod)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/social/utils.py", line 15, in import_module
__import__(name)
ImportError: No module named auth
Also in my settings.py I have the following code. 同样在我的settings.py中,我有以下代码。 If I comment out the
'social.apps.django_app.me',
I will not have a database connected with the social-auth. 如果我注释掉
'social.apps.django_app.me',
则不会有与social-auth连接的数据库。 If I leave it, the code fails. 如果我离开它,代码将失败。
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'mongoengine.django.mongo_auth',
'social.apps.django_app.default',
'social.apps.django_app.me', # this is the line that fails
)
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
'django.contrib.auth.backends.ModelBackend',
'social.backends.facebook.FacebookOAuth2',
)
# Engine stuff
SESSION_ENGINE = 'mongoengine.django.sessions'
#AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'
_MONGODB_USER = '***' #real stuf here
_MONGODB_PASSWD = '***' #real stuf here
_MONGODB_HOST = '***' #real stuf here
_MONGODB_NAME = '****' #real stuf here
_MONGODB_DATABASE_HOST = \
'mongodb://%s:%s@%s/%s' \
% (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)
mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)
# Auth Stuff
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.me.models.DjangoStorage'
SOCIAL_AUTH_FACEBOOK_KEY = '***' #real stuf here
SOCIAL_AUTH_FACEBOOK_SECRET = '***' #real stuf here
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email','user_location']
Do I miss something to add? 我是否想添加一些东西? How can I fix it?
我该如何解决?
Was trying this my self and seems that the default value for AUTH_USER_MODEL
is causing errors, the default value is auth.User
which refs to django.contrib.auth.models.User
. 我正在尝试此操作,似乎
AUTH_USER_MODEL
的默认值导致错误,默认值为auth.User
,它引用django.contrib.auth.models.User
。 But defining it to mongoengine.django.auth.User
makes django complain about the format not being app_name.ModelName
. 但是将其定义为
mongoengine.django.auth.User
会使django抱怨格式不是app_name.ModelName
。
Defining SOCIAL_AUTH_USER_MODEL = 'mongoengine.django.auth.User'
solves the issue. 定义
SOCIAL_AUTH_USER_MODEL = 'mongoengine.django.auth.User'
解决了此问题。
MongoEngine provides two distinct methods to provide authentication in django: MongoEngine提供了两种不同的方法来在django中提供身份验证:
Only the first one will work with python-social-auth (as of v0.1.17). 只有第一个可以使用python-social-auth(从v0.1.17开始)。
Basically, you just need to use a custom authentication backend: 基本上,您只需要使用自定义身份验证后端:
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
You shouldn't set AUTH_USER_MODEL
, as this is used to define a Custom User Model. 您不应设置
AUTH_USER_MODEL
,因为它用于定义自定义用户模型。 Here, Django's models are just ignored. 在这里,Django的模型将被忽略。
The user document class used to store users is defined by MONGOENGINE_USER_DOCUMENT
, which defaults to 'mongoengine.django.auth.User'
. 用于存储用户的用户文档类由
MONGOENGINE_USER_DOCUMENT
定义,默认为'mongoengine.django.auth.User'
。 You can update this value if you want to write your custom user class. 如果要编写自定义用户类,则可以更新此值。
Then, it's just a matter of setting the same user document class for Social-Auth, as omab pointed: 然后,就像omab指出的那样,只需为Social-Auth设置相同的用户文档类即可:
SOCIAL_AUTH_USER_MODEL = 'mongoengine.django.auth.User'
This is the option python-social-auth supports. 这是python-social-auth支持的选项。
Django 1.5 introduced a new way to define a custom user model class, which this method makes use of. Django 1.5引入了一种定义自定义用户模型类的新方法,该方法可以使用该方法。 Authentication will be done using the standard Django authentication backend:
身份验证将使用标准的Django身份验证后端进行:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
AUTH_USER_MODEL
must be set to 'mongo_auth.MongoUser'
, which does point to mongoengine.django.mongo_auth.MongoUser
(using Django's app_name.model_name
notation). 必须将
AUTH_USER_MODEL
设置为'mongo_auth.MongoUser'
,它确实指向mongoengine.django.mongo_auth.MongoUser
(使用Django的app_name.model_name
表示法)。
This MongoUser
model is not directly used (as it's a Django Model), it's just a way to tell Django to use another model using Django's Custom User Model . 这个
MongoUser
模型不是直接使用的(因为它是Django模型),它只是通过Django的Custom User Model告诉Django使用另一个模型的一种方式。 The manager on this model will simply return MongoEngine documents defined by MONGOENGINE_USER_DOCUMENT
as above. 此模型的管理者将仅返回由
MONGOENGINE_USER_DOCUMENT
定义的MONGOENGINE_USER_DOCUMENT
文档,如上所述。
Unfortunately, this method is not supported by python-social-auth, as it expects AUTH_USER_MODEL
to be a MongoEngine document, while it is actually a proxy Django Model. 不幸的是,python-social-auth不支持此方法,因为它希望
AUTH_USER_MODEL
是MongoEngine文档,而实际上它是Django代理模型。
I created a Pull Request to try and fix the issue. 我创建了一个“ 拉取请求”以尝试解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.