2017-09-25 59 views
1
from django.contrib.auth.models import User 
from django.core.management.base import BaseCommand 


class Command(BaseCommand): 
    help = 'Create default system user without login authorization' 

    def handle(self, *args, **options): 
     User.objects.create_superuser('codobot', '[email protected]', None) 

我創建具有超級用戶密碼None
但是,當我看着數據庫仍然在現場password數據。 開始!
創建一個Django的超級用戶,但從來沒有讓它登錄

問:
的是,用戶可以登錄?

+2

如果您想創建用戶並且不讓他登錄,您可以使用* is_active *將其設置爲False,並且用戶無法登錄到您的系統(如果您使用的是Django身份驗證模型)。 – Thaian

+0

首先**沒有**也可以有一個散列值,但如果django檢測到None爲raw_password,那麼它會被設置爲不可用的密碼,這就是爲什麼數據在密碼字段中的答案。 **!**只是由我猜測的密鑰哈希組合引起的。 – zypro

+0

@Thaian。謝謝您的回覆。只是意識到你的答案也是我的選擇 – Sarit

回答

2

沒有用戶無法登錄,

你可以簡單的驗證它:

from django.contrib.auth import authenticate 

user = authenticate(username='codobot', password=None) 
print ('Could not login') if user is None else ('User is logged in') 

通過文檔

當raw_password是無,密碼將被設置爲不可用密碼,就像使用了set_unusable_password()一樣。

細節here

+0

請稍等3分鐘。謝謝。 – Sarit

+0

https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.authenticate 它與'request'一起使用你怎麼知道它可以與我的情況一起使用? – Sarit

+0

啊。 **憑據。我的壞 – Sarit

相關問題