我一直在寻找答案,但遗憾的是,没有。
我正在尝试在django admin下拉列表中添加操作(其中Delete Selected
是)但是在添加了一些操作后,我添加的操作也显示在其他模型上,我已经按照{{{ 3}}但没有运气
如何限制我仅添加到特定型号的操作?
提前致谢。
(我使用的是python 2.7,django 1.8.2)
修改
我通过以下方式按用户类型(超级用户或非超级用户)过滤选项:
#This a test application only
def fire_employee(modeladmin, request, queryset):
queryset.update(APP_STATUS=8)
fire_employee.short_description = "Fire/Retire Employee"
class hrd(admin.ModelAdmin):
model = hired_applicant
def get_actions(self, request):
actions = super(hrd, self).get_actions(request)
if not request.user.is_superuser:
if 'delete_selected' in actions:
del actions['delete_selected']
else:
self.actions.append(fire_employee)
return actions
list_display = ('FIRST_NAME', 'MIDDLE_NAME', 'LAST_NAME', 'EMAIL', 'APPLICATION_DATE', 'APP_STATUS')
admin.site.register(hired_applicant, hrd)
答案 0 :(得分:0)
在看到AdminModel
课程后,我仍然不确定您是否为所有模型使用相同的课程。
@admin.site.register(ModelOne, ModelTwo)
class AdminModel(admin.ModelAdmin):
def get_actions(self, request):
actions = super(AdminModel, self).get_actions(request)
if not request.user.is_superuser:
if 'delete_selected' in actions:
del actions['delete_selected']
else:
self.actions.append(make_published)
self.actions.append(make_unpublished)
return actions
如果您只需要针对特定模型的操作,则只需制作两个单独的ModelAdmin
类
@admin.site.register(ModelOne)
class AdminModel(admin.ModelAdmin):
def get_actions(self, request):
actions = super(AdminModel, self).get_actions(request)
if not request.user.is_superuser:
if 'delete_selected' in actions:
del actions['delete_selected']
else:
self.actions.append(make_published)
self.actions.append(make_unpublished)
return actions
@admin.site.register(ModelTwo)
class NoActionsModel(admin.ModelAdmin):
pass
此外,ModelAdmin
您应该可以使用has_delete_permission
(docs)功能阻止所有超级用户
class AdminModel(admin.ModelAdmin):
def has_delete_permission(self, request, obj=None):
return request.user.is_superuser