在扩展CB上按用户显示内容筛选器

2024-03-28 23:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在ListView上显示当前登录用户发布的仅包含内容的帖子,但这里的答案:How to make generic ListView only show user's listing?无法解决我的问题,因为我的ListView使用了4个模型

视图.py

class DashboardListView(LoginRequiredMixin,ListView):
model = Links
template_name = 'dashboard/home.html'
context_object_name ='links_list'
paginate_by = 15

def get_context_data(self, **kwargs):

    context = super().get_context_data(**kwargs)

    context['dashboard_list']= Dashboard.objects.all()[:15]
    context['todo_list']= Todo.objects.all().order_by('-pk')[:15]
    context['todo_complete']= Todo.objects.all().count()
    context['PasswordUsername_list']= PasswordUsername.objects.all()
    return context

def get_queryset(self):
    return self.model.objects.filter(author=self.request.user)

我试图添加一个get\u context和一个get\u query\u set,但它只隐藏链接模型

谢谢


Tags: name模型selfdatagetbymodelobjects
1条回答
网友
1楼 · 发布于 2024-03-28 23:24:51

多亏了另一篇帖子,我才明白

视图中.py

class DashboardListView(LoginRequiredMixin,ListView):
model = Links
template_name = 'dashboard/home.html'
context_object_name ='links_list'
paginate_by = 15

def get_queryset(self):
    return self.model.objects.filter(author=self.request.user) 

def get_context_data(self, **kwargs):

    context = super().get_context_data(**kwargs)

    context['dashboard_list']= Dashboard.objects.filter(author=self.request.user)[:15]
    context['todo_list']= Todo.objects.filter(author=self.request.user).order_by('-pk')[:15]
    context['PasswordUsername_list']= PasswordUsername.objects.filter(author=self.request.user)
    return context

您必须使用“yourmodel.objects.filter(author=self.request.user)”筛选上下文 和一个查询集

def get_queryset(self):
    return self.model.objects.filter(author=self.request.user) 

相关问题 更多 >