2013-05-25 38 views
12

Tôi đang sử dụng ListAPIView, nhưng tôi không thể lọc kết quả. Mã của tôi là:Danh sách lọcAPIView trong django-rest-framework

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 

Trong trường hợp này, tài liệu này cho biết rằng nó cũng được hỗ trợ cho lớp này. Nếu tôi cố gắng triển khai get tùy chỉnh theo chế độ xem chung, tôi không biết cách thực hiện lại paginate_by. Bất kỳ ý tưởng?

Trả lời

19

tôi đã tìm thấy các giải pháp

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') 

Nguồn: http://www.django-rest-framework.org/api-guide/filtering/#filtering-against-the-url

3

Tôi biết là muộn cho điều này, nhưng tôi đã viết một ứng dụng nhỏ mà kéo dài cho ListAPIView và làm điều này dễ dàng hơn, check it out:

https://github.com/angvp/drf-lafv

Các vấn đề liên quan