[英]Django Rest Framework combine multiple querysets in one api endpoint
如何连接特定模型的列表的输出,其实例由不同的字段过滤? 例如,我有一个 Places 模型和两个不同的 url。 一种是显示整个列表,另一种是仅显示 new_place = True 的实例。 使用 django-filter 做一个 API。
模型.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")
序列化程序.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',)
网址.py
url(r'^new/$', views.PlacesNewListAPIView.as_view(), name = 'list_new'),
url(r'^$', views.PlacesListAPIView.as_view(), name = 'place_list'),
这一切都适用于不同的网址。 但是我应该怎么做才能在一页上获得两个列表。 顶部的 NEW 列表和页面底部的完整列表?
你可以做这样的事情。 使用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)
您可以使用 Places.objects.order_by('-new_place') 更新 PlacesListAPIView 中的查询。 它将在顶部放置新位置,其他位置在底部
class PlacesListAPIView(ListAPIView):
queryset = Places.objects.order_by('-new_place')
serializer_class = PlaceSerializer
新地点列表不需要单独的 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.