gpt4 book ai didi

python - Django "list_filter"基于管理查询集

转载 作者:行者123 更新时间:2023-12-01 08:05:09 27 4
gpt4 key购买 nike

我正在开发一个同时具有管理员和子管理员的应用程序。子管理员是根据他们所属的组创建的。因此,子管理员只能看到与其组相关的数据。此功能运行完美。

现在,我想创建一个基于用户的过滤器选项。对于 super 管理员来说,它工作正常,但是当我从子管理员那里查看时,我可以在过滤器列表中看到完整的用户列表。如何根据子管理员用户限制此列表。

假设一共有20个用户,而子管理员的组里只有3个用户。因此,我在 ListView 中只能看到 3 个用户,但在过滤器选项中我可以看到全部 20 个用户。我可以将此过滤器限制为仅限这 3 个用户吗?

我的管理模型如下所示:

class VideoDetailsAdmin(admin.ModelAdmin):
list_display = ('video_id', 'user', 'status', 'description', 'video_name', 'upload_time', 'duration')

list_filter = ('user', )

def get_queryset(self, request):
# Get the groups of logged in user
query_set = Group.objects.filter(user = request.user)
group_list = []
for g in query_set:
group_list.append(g.name)
# Get the user ids of Users in those groups
id_list = []
for user in list(User.objects.filter(groups__name__in=group_list)):
id_list.append(user.id)
# Create unique users list
users = list(set(id_list))
# Override the get_queryset method for Admin
qs = super(VideoDetailsAdmin, self).get_queryset(request)

if not request.user.is_superuser:
return qs.filter(user__in = users)
else:
return qs

我在 Django 上看到的可用资源很少 documentation ,但我想知道解决这个问题的最佳方法是什么。有没有办法可以重用 get_queryset() 代码

Django 版本:2.1

编辑1:

进行了以下修改,但没有可见过滤器。

# Filter list
class UserFilterList(admin.SimpleListFilter):
# Human readable title, which is displayed on the right sidebar
title = ("User")

# Parameter for the filter that will be used in the URL query
parameter_name = "user"

def lookups(self, request, model_admin):
# To get user's groups
query_set = Group.objects.filter(user = request.user)
group_list = []
for g in query_set:
group_list.append(g.name)
# To get all users associated in those groups
id_list = []
for user in list(User.objects.filter(groups__name__in=group_list)):
id_list.append(user.id)

users = list(set(id_list))
qs = model_admin.get_queryset(request)

def queryset(self, request, queryset):
if not request.user.is_superuser:
return qs.filter(user__in = users)
else:
return qs

编辑2:

class UserFilterList(SimpleListFilter):
title = "user"
parameter_name = "user"
def lookups(self, request, model_admin):
visible_users = model_admin.get_visible_users(request)
print(visible_users[0])
return ((user, user) for user in visible_users)

def queryset(self, request, queryset):
return self.value()

最终工作解决方案(感谢@dirkgroten):

class UserFilterList(SimpleListFilter):
title = "user"
parameter_name = "user"
def lookups(self, request, model_admin):
if not request.user.is_superuser:
visible_users = model_admin.get_visible_users(request)
# Sub user - return same group users
return ((user.id, user) for user in visible_users)
else:
# Superuser - return all users
return ((user.id, user) for user in User.objects.filter())

def queryset(self, request, queryset):
return queryset.filter(user=self.value()) if self.value() else queryset

class VideoDetailsAdmin(admin.ModelAdmin):
list_display = ('video_id', 'user', 'status', 'description', 'video_name', 'upload_time', 'duration')

list_filter = (UserFilterList, )

def get_visible_users(self, request):
# Get the groups of logged in user
query_set = Group.objects.filter(user = request.user)
group_list = []
for g in query_set:
group_list.append(g.name)
return User.objects.filter(groups__name__in=group_list)

def get_queryset(self, request):
users = self.get_visible_users(request)

# Override the get_queryset method for Admin
qs = super(VideoDetailsAdmin, self).get_queryset(request)

if not request.user.is_superuser:
return qs.filter(user__in = users)
else:
return qs

最佳答案

不要更改您的VideoDetailsAdmin,仅使用自定义列表过滤器:

class VideoDetailsAdmin(ModelAdmin):
list_filter = UserFilterList # that's the only line to change

def get_visible_users(self, request): # small refactor to re-use in filter
query_set = Group.objects.filter(user=request.user)
group_list = []
for g in query_set:
group_list.append(g.name)
# To get all users associated in those groups
return User.objects.filter(groups__name__in=group_list)

def get_queryset(self, request):
users = self.get_visible_users(request)
# Override the get_queryset method for Admin
qs = super(VideoDetailsAdmin, self).get_queryset(request)

if not request.user.is_superuser:
return qs.filter(user__in=users)
else:
return qs

class UserFilterList(SimpleListFilter):
def lookups(self, request, model_admin):
visible_users = model_admin.get_visible_users(request)
return ((user.pk, user.username) for user in visible_users)

def queryset(self, request, queryset):
return queryset.filter(user_id=self.value()) if self.value() else queryset

关于python - Django "list_filter"基于管理查询集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55574344/

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