在Django中为多个查询编写视图的最佳方法? [英] Best way to write views for multiple queries in Django?

查看:47
本文介绍了在Django中为多个查询编写视图的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题.我已经对模型进行了组织,以便为页面提供的大多数对象都是一种类型-项目.该模型包含各种属性,可以帮助我以不同的方式为其提供服务.

It's a simple question. I've organised my models so that most objects served to the page are of one type - Item. The model contains various attributes which help me serve it in different ways.

我有文章和视频,它们由模型上的类型"字段确定.类型='商品'等

I have articles, and videos, which are determined by a 'type' field on the model. Type = 'article' etc.

我有一个列表视图,该列表视图显示了Item模型中的所有对象,并按日期排序.

I have a listview, which shows all the objects in the Item model, sorted by date.

class ItemListView(generic.ListView):

    # This handles the logic for the UserForm and ProfileForm - without it, nothing happens.
    def item(self, request, *args, **kwargs):
        return index(request)

    def get_queryset(self):
        # return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
        return Item.objects.all().order_by('-create_date')

我想要一个视图,该视图显示按日期排序的所有文章,以及按日期排序的所有视频.我觉得我会写更多这样的观点.

I want a view which shows all the articles, sorted by date, and all the videos, sorted by date. I have a feeling I'll be writing many more such views.

是否有比为每个查询编写一个新视图更好的方法?因为,这就是我目前正在做的事情:

Is there a better way to do it than to write a new view for every single query? As, this is what I'm currently doing:

Views.py

class ItemListViewArticle(generic.ListView):

    # This handles the logic for the UserForm and ProfileForm - without it, nothing happens.
    def item(self, request, *args, **kwargs):
        return index(request)

    def get_queryset(self):
        # return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
        return Item.objects.filter(type__contains='article').order_by('-create_date')

class ItemListViewVideo(generic.ListView):

    # This handles the logic for the UserForm and ProfileForm - without it, nothing happens.
    def item(self, request, *args, **kwargs):
        return index(request)

    def get_queryset(self):
        # return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
        return Item.objects.filter(type__contains='video').order_by('-create_date')

urls.py

    path('new/', views.ItemListView.as_view(), name='new_list'),
    path('new/articles', views.ItemListViewArticle.as_view(), name='article_list'),
    path('new/videos', views.ItemListViewVideo.as_view(), name='video_list'),

推荐答案

您可以使用 URL查询字符串(即 请求.GET )以从url获取项目的类型并对其进行过滤.像这样:

You can use URL querystring(ie request.GET) to get type of the item from url and filter by it. Like this:

path('new/', views.ItemListView.as_view(), name='new_list'),


class ItemListViewArticle(generic.ListView):

    def item(self, request, *args, **kwargs):
        return index(request)

    def get_queryset(self):
        content_type = self.request.GET.get('type')
        return Item.objects.filter(type__contains=content_type).order_by('-create_date')

# usage
localhost:8000/new/?type=article
localhost:8000/new/?type=video

或者您可以使用 URL参数来获取类型数据:

Or you can use URL parameter to get the type of data:

path('new/<str:content_type>/', views.ItemListView.as_view(), name='new_list'),

class ItemListViewArticle(generic.ListView):

    def item(self, request, *args, **kwargs):
        return index(request)

    def get_queryset(self):
        content_type = self.kwargs.get('content_type')
        return Item.objects.filter(type__contains=content_type).order_by('-create_date')

# usage
localhost:8000/new/article/
localhost:8000/new/video/

这篇关于在Django中为多个查询编写视图的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆