我有一个Django服务器(使用PostGis),我想禁用与身份验证相关的所有内容:
它确实得到了我希望的结果,直到我尝试通过管理员添加对象。然后我得到一个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这样的解决方案来解决它,但它并没有帮助。
只要获得最终目标,我不介意采用全新方法。
非常感谢,
答案 0 :(得分:3)
由于Django项目在dockers中运行,并且可以在用户已经存在时部署或者我没有最终完成:
# 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')
希望有一天能帮助别人。完全使用:
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')