本文介绍了Python-Social-Auth无法与mongoEngine(Django)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使 python-social-auth 使用 mongodb

我按照的说明添加说明:

I follow the instructions here that say to add:

INSTALLED_APPS = (
    ...
    'social.apps.django_app.me',
    ...
)

SOCIAL_AUTH_STORAGE = 'social.apps.django_app.me.models.DjangoStorage'

但是出现错误,我得到一个ImportError:

However something goes wrong and I get an 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

另外在我的settings.py我有以下代码。如果我注释了'social.apps.django_app.me',我将不会有一个数据库与社会认证相关联。如果我离开,代码将失败。

Also in my settings.py I have the following code. If I comment out the 'social.apps.django_app.me', I will not have a database connected with the 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?

推荐答案

正在尝试这个我的自我,似乎 AUTH_USER_MODEL的默认值导致错误,默认值为 auth.User ,其中引用 django.contrib.auth.models.User 。但是将其定义为 mongoengine.django.auth.User 使django抱怨格式不是 app_name.ModelName

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. But defining it to mongoengine.django.auth.User makes django complain about the format not being app_name.ModelName.

定义 SOCIAL_AUTH_USER_MODEL ='mongoengine.django.auth.User'解决问题。

这篇关于Python-Social-Auth无法与mongoEngine(Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 00:38
查看更多