Connecting Social Auth to MongoEngine models gives errors

800 Views Asked by At

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.

0

There are 0 best solutions below