閱讀文檔讓我明白,Django中的默認權限系統與模型相關,這是可以理解的,因爲它是爲管理界面創建的。Django權限取決於模型?
這是什麼意思?如果我只使用視圖權限,與權限連接的模型會不會被使用?
這是爲了使這個問題更具體。我的目標是將視圖限制在一個特定的用戶組中。
創建的ContentType:
content_type = ContentType.objects.get(app_label='myapp', model='SomeModel')
創建權限:
permission = Permission.objects.create(codename='can_view_this', name='Can Access this View', content_type=content_type)
創建一個組:
group = Group.objects.get_or_create(name='some_group')
添加權限組:
group.permissions.add(permission)
將用戶添加到組:
group.user_set.add(User.objects.get(id=1))
試驗許可:
@user_passes_test(lambda u: u.has_perm('myapp.can_view_this'))
def some_view(request):
所以回到步驟1,通知我如何分配模型才能創建ContentType, nd我不得不創建一個ContentType,因爲創建一個Permission條目需要一個ContentType。
所以問題是,在這種情況下,SomeModel
會被再次使用嗎?我甚至不確定究竟哪個模型分配了特定於視圖的權限,沒有任何意義。
希望這個問題是有道理的。