gpt4 book ai didi

python - Django 管理操作的权限

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:46 24 4
gpt4 key购买 nike

我已经为我的 Django 项目编写了一些自定义操作,但是无法弄清楚如何让它们只对 super 用户可用。我尝试在 Users.is_superuser 的操作行周围放置一个 if 语句,但它总是给我一个错误,说没有名为 is_superuser 的属性。

这是我的 admin.py 文件:

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)

最佳答案

您可以通过覆盖 get_actions() 来自定义操作列表。例如:

class ArtAdmin(ImportExportModelAdmin):
list_display = ['id', 'identification', 'name', 'artist', 'category', 'type', 'agent', 'authenticate', ]
search_fields = ('name', 'category', 'artist', 'id', 'authenticate', )
list_filter = ["authenticate"]
actions = [approve_art, reject_art]

def get_actions(self, request):
actions = super(ArtAdmin, self).get_actions(request)
if not request.user.is_superuser:
del actions[approve_art]
del actions[reject_art]
return actions

查看 https://docs.djangoproject.com/en/1.9/ref/contrib/admin/actions/#conditionally-enabling-or-disabling-actions了解更多信息

关于python - Django 管理操作的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38329999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com