Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticate in Django without a database

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

like image 208
gosling_ Avatar asked Apr 11 '16 18:04

gosling_


People also ask

Can I use Django without database?

However, while you can use django with no database, the object-relational mapper is pretty much its first and foremost advertised feature. Django was designed to produce database-backed web sites, so if you're not going to use a database you might end up dealing with a bunch of unnecessary hassle.

How do I authenticate a user in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...

How does authenticate work in Django?

The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.


1 Answers

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.

like image 155
bignose Avatar answered Oct 19 '22 09:10

bignose