I have written some custom actions for my django project, however cannot work out how to make them available to only superusers. I have tried putting an if statement round the actions line with Users.is_superuser but it keeps giving me an error saying there is no attribute called is_superuser.
Here is my admin.py file:
from django.contrib import admin
from models import Art, Agent, UserProfile
from django.contrib import admin
from django.contrib.auth.models import Group, User, AbstractUser
from django.contrib.auth import *
from import_export import resources
from import_export.admin import ImportExportModelAdmin
#admin.site.unregister(Group)
def approve_art(modeladmin, request, queryset):
queryset.update(authenticate = "approved")
def reject_art(modeladmin, request, queryset):
queryset.update(authenticate = "rejected")
# Add in this class to customized the Admin Interface
class ArtAdmin(ImportExportModelAdmin):
list_display = ['id', 'identification', 'name', 'artist', 'category', 'type', 'agent', 'authenticate', ]
search_fields = ('name', 'category', 'artist', 'id', 'authenticate', )
actions = [approve_art, reject_art]
list_filter = ["authenticate"]
class AgentAdmin(admin.ModelAdmin):
list_display = ['id', 'name', 'phone', 'postcode', ]
search_fields = ('name', 'id', )
class ArtResource(resources.ModelResource):
class Meta:
model = Art
# Update the registeration to include this customised interface
admin.site.register(Art, ArtAdmin)
admin.site.register(Agent, AgentAdmin)
5条答案
按热度按时间uqcuzwp81#
您可以通过覆盖
get_actions()
来自定义操作列表。例如:有关https://docs.djangoproject.com/en/1.9/ref/contrib/admin/actions/#conditionally-enabling-or-disabling-actions详细信息,请访问www.example.com
mwkjh3gx2#
更新Django〉= 2.1
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/actions/#setting-permissions-for-actions
简而言之:
或自定义:
(示例代码全部取自链接文档。)
uidvcgyl3#
考虑到操作不是
ModelAdmin
相关的,防止它被未授权用户运行的最佳方法仍然是在操作内部检查它:这就是django在
delete_selected
操作中处理它的方式。虽然下拉列表中的操作仍然可用,但会返回403 HTTP代码。
wbgh16ku4#
您可以像这样覆盖ModelAdmin的
get_actions
方法:这里是您可能想要查看的文档资料。
w7t8yxp55#
仅对于超级用户,您可以通过覆盖get_actions()来显示**
approve_art
和reject_art
管理操作**,如下所示: