Django Rest Framework将多个查询集合在一个api端点中

时间:2018-05-08 17:03:17

标签: django django-rest-framework

如何连接特定模型列表的输出,其实例按不同字段过滤?例如,我有一个Places模型和两个不同的URL。在一个中,显示整个列表,而在另一个中仅显示new_place = True的实例。 使用django-filter做一个API。

models.py

class Places(models.Model):
    main_photo = models.ImageField(upload_to = 'album/')
    place = models.CharField(max_length=100)
    new_place = models.BooleanField(default = True)
    editor_choice = models.BooleanField(default = False, verbose_name = "Editors choice")
    most_popular = models.BooleanField(default = False, verbose_name = "Most popular")

serializer.py

class PlaceSerializer(ModelSerializer):

    url = HyperlinkedIdentityField(
    view_name='places_api:detail',
    lookup_field='pk'
    )
    class Meta:
        model = Places
        fields = (
            'url',
            'id',
            'main_photo',
            'name',
            'new_place',
            'most_popular', 
            'editor_choice',                        
        )

full_list_views.py

class PlacesListAPIView(ListAPIView):
    queryset = Places.objects.all()
    serializer_class = PlaceSerializer

new_places_views.py

class PlacesNewListAPIView(ListAPIView):
    queryset = Places.objects.all()
    serializer_class = PlaceSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('new_place',)

urls.py

url(r'^new/$', views.PlacesNewListAPIView.as_view(), name = 'list_new'),
url(r'^$', views.PlacesListAPIView.as_view(), name = 'place_list'),

这一切都适用于不同的网址。但是,如何在一个页面上获取两个列表呢?顶部的NEW列表和页面底部的完整列表?

UPD
在工作状态下,它看起来像http://joxi.ru/p27nkwbH0M4bXm

2 个答案:

答案 0 :(得分:2)

你可以这样做。使用QuerySet.union()合并多个查询集。

此示例不支持分页。我怀疑如果你需要分页,你必须写一个自定义的分页类。

class MultiFilterPlacesListView(ListAPIView):
     """Custom queryset api view. Does not implement pagination"""

     pagination_class = None
     queryset = Places.objects.all()
     slice_size = 10  # count limit for each of the source queries

     def get_queryset(self):
         """Combine queries from new, editor choice and popular"""
         new_qs = self.queryset.filter(new_place=True)[:self.slice_size]
         editor_qs = self.queryset.filter(editor_choice=True)[:self.slice_size]
         popular_qs = self.queryset.filter(popular=True)[:self.slice_size]

         return new_qs.union(editor_qs, popular_qs, all=True)

答案 1 :(得分:0)

您可以使用Places.objects.order_by(' - new_place')更新PlacesListAPIView中的查询。它会将新位置放在顶部,其他位置放在底部

class PlacesListAPIView(ListAPIView):
    queryset = Places.objects.order_by('-new_place')
    serializer_class = PlaceSerializer

新地点列表无需单独的网址。