Create a module auto_auth.py
:
from django.contrib.auth.models import User
from django.utils.deprecation import MiddlewareMixin
class AutoAuthMiddleware(MiddlewareMixin):
def process_request(self, request):
request.user = User.objects.filter()[0]
Edit MIDDLEWARE
in your settings.py
:
- Remove
'django.contrib.auth.middleware.AuthenticationMiddleware'
- Add
'auto_auth.AutoAuthMiddleware'
You can change User.objects.filter()[0]
to something else if you want a particular user.
In response to your comment: yes. To run the Django admin without users at all, try this:
class User:
is_superuser = True
is_active = True
is_staff = True
id = 1
def return_true(*args, **kwargs):
return True
User.has_module_perms = return_true
User.has_perm = return_true
class AutoAuthMiddleware(MiddlewareMixin):
def process_request(self, request):
request.user = User()
And remove 'django.contrib.auth'
from INSTALLED_APPS
But if you use any apps that depend on the auth app, you're going to have a bad time.