1

I'm not really experienced in Django and graph databases. But I need to set up user authentication with MongoDB in Django.

I'm using pymongo for connection to database bypassing Django db settings. But I still want to use Django authentication. In order to do so, I actually should create a user model extended from AbstractBaseUser, create custom user manager extended from BaseUserManager, and register AUTH_USER_MODEL in settings.

I've also created and registered custom backend which returns my custom User object.

But the problem is that it seems like Django tries to verify my User model using its database (sqlite in settings) and it can't find USERNAME_FIELD (because actually I'm not using this db. I don't know how to tell Django that it don't need to care about it.

Can you provide any suggestions, please?

shaurun
  • 70
  • 6
  • Hi @sharun - hopefully that answer helped. Please up-vote if it's not too much trouble as well. :) –  Aug 25 '18 at 20:46
  • Hi @MichealJRoberts, I'd like to upvote you, but actually I don't have enough reputation to do it. When I would, I'll surely do) – shaurun Aug 26 '18 at 18:54

2 Answers2

3

It looks like you need to tell Django to explicitly use MongoDB as it's backend database. There are many tutorials online, however, the best one I can recommend from experience is "djongo".

(venv) $ pip install djongo && pip freeze > requirements.txt

If you don't have a virtual environment installed, if not just simply use:

$ pip install djongo

And then in settings.py:

DATABASES = {
   ‘default’: {
      ‘ENGINE’: ‘djongo’,
      ‘NAME’: ‘your-db-name’,
   }
}

And that is it! Simple as that, in theory. (Minus any extra database configurations you want to add).

To circumnavigate the USERNAME_FIELD issue, simply use something like this in your extended User class:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
    is_active = models.BooleanField(_('active'), default=True)
    avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)

    objects = UserManager() # <-- THIS IS YOUR CUSTOM USER MANAGER CLASS

    USERNAME_FIELD = 'email' # <-- INCLUDE THIS LINE HERE!
    REQUIRED_FIELDS = []
0

Here is a post where I answered a similar question. If you go down to the user class you will USERNAME_FIELD = 'email' Set this to what ever you want to use as the username field. If you are not going to use sqlite and do not need the data delete that file.

If you haven't set up your settings to use mongodb then using djongo is a good choice. just pip install djongo and then add

DATABASES = {
   ‘default’: {
      ‘ENGINE’: ‘djongo’,
      ‘NAME’: ‘your-db-name’,
   }
}

to you settings.py. Here is a tutorial about djongo

Taylor
  • 1,223
  • 1
  • 15
  • 30