2017-02-09 120 views
1

我有一個Django的服務器(使用PostGIS的),我想禁用所有與身份驗證:如何完全禁用身份驗證Django管理

  • 當進入無認證將被要求
  • 在管理員管理員隱藏用戶/組

網上搜索後,我嘗試的組合this & this

它確實爲我帶來了我希望的結果,直到我嘗試通過管理員添加對象。然後,我得到一個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它解決,並沒有幫助。

只要最終目標被獲得,我並不介意以全新的方式解決問題。提前

感謝,

+3

您需要實際創建一個id = 1的用戶。 –

回答

2

由於Django項目在碼頭工人運行,且當用戶已經存在或部署不我落得這樣做:

# 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]', '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]', '123456')