扩展DJANGO的LISTVIEW

不用MODEL,不用QUERYSET,而用get_queryset方法来扩展LISTVIEW,

从而实现特定过滤或搜索功能。

class DVListView(ListView):
    template_name = 'DV/list_DVversion.html'
    # model = DV
    # queryset = DV.objects.all()
    paginate_by = 10

    def get_queryset(self):
        print self.kwargs
        if self.kwargs:
            if self.kwargs.has_key('user_pk'):
                return DV.objects.filter(allow_user=self.kwargs['user_pk'])
            if self.kwargs.has_key('app_pk'):
                return DV.filter(app_name=self.kwargs['app_pk'])
            if self.kwargs.has_key('search_pk'):
                return DV.objects.filter(name__contains=self.kwargs['search_pk'])
        else:
            return DeployVersion.objects.all()

    def get_context_data(self, **kwargs):
        context = super(DVListView, self).get_context_data(**kwargs)
        context['form'] = SearchForm
        context['now'] = timezone.now()
        context['user_id'] = self.request.user
        context['current_page'] = "DV-list"

        return context

 

扩展DJANGO的LISTVIEW