2013-06-27 61 views
3

我已經爲此摔跤了幾天了。 django.contrib.auth.models有一個允許多個權限的用戶模型,但是對於is_staff,is_superuser和is_active有3個標誌。爲什麼Django使用is_staff和is_superuser而不是使用這些權限?

is_staff = models.BooleanField(_('staff status'), default=False, help_text=_('Designates whether the user can log into this admin site.')) 
is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.')) 
is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_('Designates that this user has all permissions without explicitly assigning them.')) 
groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True, help_text=_('The groups this user belongs to. A user will get all permissions granted to each of his/her group.')) 
user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True, help_text='Specific permissions for this user.') 

什麼我不明白的是爲什麼is_staff和is_superuser都沒有權限,你可以與大約相同數量的代碼的方式實現它,但出於某種原因,他們選擇移動這兩個領域的布爾在模型上。

我問的部分原因是因爲我有類似的情況,可能會使用第三個布爾值(或者我可以用權限來做),我試圖理解他們爲什麼會將這兩個從權限?

我想我明白IS_ACTIVE,它允許你鎖定的人出來,而不必當他們返回到「修理」他們的權限(雖然你可以有一個is_locked_out「許可」,但只是聽起來是錯誤的。)

回答

4

我會猜測性能。檢查一個布爾型字段要便宜得多,然後必須從另一個表中獲取與用戶相關的權限,或者執行sql連接。考慮到在某些情況下,每個請求都會檢查用戶授權狀態,因此將布爾值放在用戶模型上並進行檢查會更明智(儘管不是那麼幹淨)。

+0

另外,'has_perm'對['is_active']有依賴性(https://docs.djangoproject.com/en/1.0/topics/auth/#django.contrib.auth.models.User.is_active)。此外,這些查找幾乎發生在每個請求上,因爲中間件使用這些屬性。 – karthikr

+0

另外,當你知道當時你正在定義你的表格時,你需要跟蹤哪些信息(比如這些布爾值),使用數據庫提供的最原始表示法總是一個好習慣。否則,您將前往輸入屬性值反模式。權限是嚴格規範化的一個可接受的細分,因爲你不知道所需的全部集合,但是如果你從一開始就知道一些用戶將是超級用戶,而另一些則不是,用戶表上的布爾列是正確的設計。擁抱數據庫模式。 –

相關問題