使 Django 管理中的权限方法更干
Make permission methods in Django admin more DRY
我的 admin.py
中有两个 ModelAdmin
。
每个都有完全相同的权限方法(has_view_permission
、has_add_permission
、has_change_permission
、has_delete_permission
、has_module_permission
)。
有什么方法可以让它更干吗?
admin.py
:
class FirstAdmin(admin.ModelAdmin):
list_display = (
'id',
'title',
'author'
)
def has_view_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_add_permission(self, request):
if request.user.is_admin:
return True
def has_change_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_delete_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_module_permission(self, request):
if request.user.is_admin:
return True
class SecondAdmin(admin.ModelAdmin):
list_display = (
'id',
'category',
)
def has_view_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_add_permission(self, request):
if request.user.is_admin:
return True
def has_change_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_delete_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_module_permission(self,request):
if request.user.is_admin:
return True
是的,您可以使用 class mixin 轻松做到这一点,我还设法将方法主体减少到一行。
方法如下:
class AdminUserPermissionMixin:
def has_view_permission(self, request, obj=None):
return request.user.is_admin
def has_add_permission(self, request):
return request.user.is_admin
def has_change_permission(self, request, obj=None):
return request.user.is_admin
def has_delete_permission(self, request, obj=None):
return request.user.is_admin
def has_module_permission(self, request):
return request.user.is_admin
class FirstAdmin(AdminUserPermissionMixin, admin.ModelAdmin):
list_display = (
'id',
'title',
'author'
)
class SecondAdmin(AdminUserPermissionMixin, admin.ModelAdmin):
list_display = (
'id',
'category',
)
我的 admin.py
中有两个 ModelAdmin
。
每个都有完全相同的权限方法(has_view_permission
、has_add_permission
、has_change_permission
、has_delete_permission
、has_module_permission
)。
有什么方法可以让它更干吗?
admin.py
:
class FirstAdmin(admin.ModelAdmin):
list_display = (
'id',
'title',
'author'
)
def has_view_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_add_permission(self, request):
if request.user.is_admin:
return True
def has_change_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_delete_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_module_permission(self, request):
if request.user.is_admin:
return True
class SecondAdmin(admin.ModelAdmin):
list_display = (
'id',
'category',
)
def has_view_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_add_permission(self, request):
if request.user.is_admin:
return True
def has_change_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_delete_permission(self, request, obj=None):
if request.user.is_admin:
return True
def has_module_permission(self,request):
if request.user.is_admin:
return True
是的,您可以使用 class mixin 轻松做到这一点,我还设法将方法主体减少到一行。
方法如下:
class AdminUserPermissionMixin:
def has_view_permission(self, request, obj=None):
return request.user.is_admin
def has_add_permission(self, request):
return request.user.is_admin
def has_change_permission(self, request, obj=None):
return request.user.is_admin
def has_delete_permission(self, request, obj=None):
return request.user.is_admin
def has_module_permission(self, request):
return request.user.is_admin
class FirstAdmin(AdminUserPermissionMixin, admin.ModelAdmin):
list_display = (
'id',
'title',
'author'
)
class SecondAdmin(AdminUserPermissionMixin, admin.ModelAdmin):
list_display = (
'id',
'category',
)