嘿,我有一个listView输出创建的事件列表。如何根据超级用户进行过滤?
基本上我希望管理员查看所有事件:关闭和打开,但我的用户只能看到打开的事件
class EventView(ListView, LoginRequiredMixin):
template_name = "chat/events.html"
model = Event
context_object_name = 'events'
def get_queryset(self):
kwargs = super(EventView, self).get_form_kwargs()
print(kwargs)
user_pk = self.kwargs['user_id']
user = User.objects.get(pk=user_pk)
print(user)
if user.is_superuser:
queryset = Event.objects.all()
return queryset
else:
queryset = Event.objects.filter(is_open=True)
return queryset
return queryset
答案 0 :(得分:0)
您非常接近,您只需要获取登录用户并相应地准备queryset:-
def get_queryset(self):
if self.request.user.is_superuser:
queryset = Event.objects.all()
else:
queryset = Event.objects.filter(is_open=True)
return queryset