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

时间:2019-02-22 01:55:35

标签: python django django-views

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

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_context get_query_set ,但是它只隐藏了链接模型。

谢谢

1 个答案:

答案 0 :(得分:0)

我想通了另一篇文章

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)