我正在使用 ListAPIView,但无法过滤结果。我的代码是:
class UserPostReadView(generics.ListAPIView):
serializer_class = PostSerializer
model = serializer_class.Meta.model
queryset = model.objects.order_by('-post_time')
lookup_field = 'poster_id'
paginate_by = 100
在这种情况下,
lookup_field
被忽略,但文档说它也支持这个类。如果我尝试实现自定义 get
在通用 View 中,我不知道如何重新实现 paginate_by
.有任何想法吗?
最佳答案
我找到了解决方案
class UserPostsReadView(generics.ListAPIView):
serializer_class = PostSerializer
model = serializer_class.Meta.model
paginate_by = 100
def get_queryset(self):
poster_id = self.kwargs['poster_id']
queryset = self.model.objects.filter(poster_id=poster_id)
return queryset.order_by('-post_time')
来源:http://www.django-rest-framework.org/api-guide/filtering/#filtering-against-the-url
关于django - 在 django-rest-framework 中过滤 ListAPIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16753173/