gpt4 book ai didi

python - 在扩展 CB 上按用户显示内容过滤器

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

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

views.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_contextget_query_set 但它只隐藏了链接模型。

谢谢

最佳答案

多亏了另一篇文章,我才弄清楚

views.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)”过滤上下文和一个query_set

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

关于python - 在扩展 CB 上按用户显示内容过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54818961/

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