如何完全禁用Django管理员的身份验证 [英] How to disable authentication altogether for Django admin

查看:251
本文介绍了如何完全禁用Django管理员的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django服务器(使用PostGis),我想禁用所有与身份验证有关的内容:

I have a Django server (Using PostGis) and I want to disable everything related to authentication:

  • 进入管理员后,无需身份验证
  • 在管理员中隐藏用户/组

在线搜索后,我尝试了

After searching online I tried the combination of this & this

它确实获得了我希望的结果,直到我尝试通过管理员添加对象为止.然后我得到一个IntegrityError:

It does get me the result I hoped for, until I try to add an object via the admin. Then I get an IntegrityError:

insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_c564eba6_fk_auth_user_id"
DETAIL:  Key (user_id)=(1) is not present in table "auth_user".

我尝试使用之类的解决方案来解决它,但没有帮助.

I tried solving it using solutions like this and it didn't help.

只要获得最终目标,我都不介意采用全新的解决方案.

I don't mind having a solution in a whole new approach as long as the end goal is acquired.

谢谢,

推荐答案

由于Django项目正在dockers中运行,并且可以在用户已经存在或我最终不这样做时进行部署:

As the Django project is running in dockers, and can be deployed when the users already exist or don't I ended up doing:

# Create superuser for admin use in case it doesn't exist
try:
    User.objects.get_by_natural_key('admin')
except User.DoesNotExist:
    User.objects.create_superuser('admin', 'admin@comapny.com', '123456')

希望这对某人有帮助.充分利用:

Hope this helps someone one day. Full use:

from django.contrib import admin
from django.contrib.auth.models import User, Group


# We add this so no authentication is needed when entering the admin site
class AccessUser(object):
    has_module_perms = has_perm = __getattr__ = lambda s,*a,**kw: True

admin.site.has_permission = lambda r: setattr(r, 'user', AccessUser()) or True

# We add this to remove the user/group admin in the admin site as there is no user authentication
admin.site.unregister(User)
admin.site.unregister(Group)

# Create superuser for admin use in case it doesn't exist
try:
    User.objects.get_by_natural_key('admin')
except User.DoesNotExist:
    User.objects.create_superuser('admin', 'admin@optibus.co', '123456')

这篇关于如何完全禁用Django管理员的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆