我有一个 Django 服务器(使用 PostGis),我想禁用与身份验证相关的所有内容:
- 进入管理员时无需进行身份验证
- 在管理中隐藏用户/组
在网上搜索后我尝试了以下组合this https://stackoverflow.com/a/40008282/5019818 & this https://stackoverflow.com/a/5508920/5019818
它确实得到了我所希望的结果,直到我尝试通过管理员添加对象。然后我得到一个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".
我尝试使用类似的解决方案来解决它this https://stackoverflow.com/a/34109293/5019818但这没有帮助。
只要达到最终目标,我不介意采用全新的方法来解决问题。
提前谢谢,
由于 Django 项目在 docker 中运行,并且可以在用户已经存在或不存在时进行部署,所以我最终执行了以下操作:
# 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', '[email protected] /cdn-cgi/l/email-protection', '123456')
希望有一天这可以帮助某人。充分利用:
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', '[email protected] /cdn-cgi/l/email-protection', '123456')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)