0

After doing necessary modifications in my settings.py file as explained in MongoEngine and Social Auth documentations, I added this code finally:

    AUTH_USER_MODEL = 'mongo_auth.MongoUser'
    SOCIAL_AUTH_MODELS = 'social_auth.db.mongoengine_models'

I get this error after trying to run the server:

File "/Library/Python/2.7/site-packages/social_auth/db/mongoengine_models.py", line 35,
  in <module> class UserSocialAuth(Document, UserSocialAuthMixin):
File "/Library/Python/2.7/site-packages/social_auth/db/mongoengine_models.py", line 37, in UserSocialAuth
  user = ReferenceField(USER_MODEL, dbref=True)
File "/Library/Python/2.7/site-packages/mongoengine/fields.py", line 837, in __init__
  self.error('Argument to ReferenceField constructor must be a '
File "/Library/Python/2.7/site-packages/mongoengine/base/fields.py", line 125, in error
  raise ValidationError(message, errors=errors, field_name=field_name)
  mongoengine.errors.ValidationError: Argument to ReferenceField constructor must be a
  document class or a string

If I remove SOCIAL_AUTH_MODELS, the server runs but Social Auth cannot work since it doesn't have any engine if MongoEngine is out of order.

MongoEngine's version: 0.8.3

I couldn't find the solution. How can I solve this?


SOLUTION:

If you look at the social_auth.db.mongoengine_models.py, you will see that USER_MODEL_APP will be set to a model, not a document. This model is defined as mongo_auth.MongoUser. Because ReferenceField requires a Document, not a Model (MongoUser), I had to neglect if statement and set USER_MODEL to a User document (mongo.django.auth.User) as it is already written in else statement. I removed replaced if and else statement by only those two lines:

USER_MODEL_MODULE, USER_MODEL_NAME = 'mongoengine.django.auth.User'.rsplit('.', 1)
USER_MODEL = getattr(import_module(USER_MODEL_MODULE), USER_MODEL_NAME)

Another way is removing SOCIAL_AUTH_USER_MODEL and AUTH_USER_MODEL from settings but I am not sure if MongoEngine requires AUTH_USER_MODELsetting. I didn't test it.

Emin Bugra Saral
  • 3,756
  • 1
  • 18
  • 25
  • Any trouble importing `mongo_auth.MongoUser` ? Could you try doing: `print get_model('mongo_auth', 'User')`? (`get_model` is at `django.db.models`) – omab Aug 30 '13 at 19:55
  • I added `from django.db.models import get_model print get_model('mongo_auth', 'User')` in the end of settings.py and it returned `None`. But I successfully imported this: `print get_model('mongo_auth', 'MongoUser')` Were you trying to say that? – Emin Bugra Saral Aug 30 '13 at 20:24
  • That's what I was trying to say, probably typed the comment to fast. – omab Aug 31 '13 at 22:26
  • Is `mongo_auth` defined before or after `social_auth` in your `INSTALLED_APPS` setting? – omab Aug 31 '13 at 23:03
  • They are both defined. `mongoengine.django.mongo_auth` and `social_auth`. Is it possible that maybe MongoEngine's current version does not match with Social Auth's structure because it is written that Social Auth was tested with 0.6.10? Maybe the methods used in classed do not match in parameters base like MongoEngine's method requires String but Social Auth passes a function? I am going really crazy here. – Emin Bugra Saral Sep 01 '13 at 01:34
  • Thanks for your trouble @omab , I wrote my solution above. There is a mismatch between two apps since MongoEngine requires a document but Social Auth passes a model instead. Maybe it's because of my settings but sure that was the problem. – Emin Bugra Saral Sep 01 '13 at 15:01

0 Answers0