15

I have a Django app that gets it's data completely from apis. so I don't have to use database. Session data is stored on signed cookies. I tried to code a custom User model and a custom auth backend like on the docs, but I get the following error: django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'my_app.MyUser' that has not been installed

My settings.py:

AUTH_USER_MODEL = 'my_app.MyUser'
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',
                           'my_app.backends.LoginAuthBackend',)

models.py:

class MyUser(object):
    def save(self):
        pass
    objects = None
    username = ""

Here, If a try use the AbstractUser from django instead of Object I got the following error: AttributeError: 'NoneType' object has no attribute '_meta' or the db table doesn't exit.

backends.py

class LoginAuthBackend(object):
    def authenticate(self, username=None, password=None):
        if username and password:
           try:
               response = my_auth_function(username, password)
               if response.status_code == 200:
                   token = response.get('my_key')
                   user = MyUser()
                   return user
            except MyCustomException:
                  return None

It's drives me crazy. Looks like Django that's not easy to use without a DB.

EDIT

After several of tries, a simple way to solve this is remove 'django.contrib.auth.backends.ModelBackend' from AUTHENTICATION_BACKENDS and AUTH_USER_MODEL from settings. The model continues basically the same way. works smoothly

gosling_
  • 151
  • 1
  • 8
  • 2
    Yes, Django is very dependent on database. It was designed this way. It looks like Django authentication system really wants you to have a User model and will not work without one. You could try harder to fool Django into accepting a fake model, but you would have to implement a whole bunch of interfaces specific to model instances and querysets. – Mad Wombat Apr 11 '16 at 18:38

1 Answers1

4

The default set of authentication back-end processors is defined in the AUTHENTICATION_BACKENDS setting. See the Django documentation for Customizing authentication.

By default, AUTHENTICATION_BACKENDS is set to:

['django.contrib.auth.backends.ModelBackend']

That’s the basic authentication backend that checks the Django users database and queries the built-in permissions.

So, if you don't want the django.contrib.auth.backends.ModelBackend authentication method, remove that from the list. You'll probably want to find (or create) a different one and add that to the list.

bignose
  • 30,281
  • 14
  • 77
  • 110