简体   繁体   中英

DjangoRestFramework - How can I use 'pk' to filter a list?

I want to filter a list of BatchLog objects according to pk that I send through the request - which is their batch_id . Is there a way to access it on views.py file?

I have this in my urls.py file

path('feed/<int:pk>', GetFeedItemView.as_view())

And in my views.py file I want to access a BatchLog object by filtering its batch_id

class GetFeedItemView(RetrieveAPIView):
    serializer_class = FeedSerializer

    def get_queryset(self):
        return BatchLog.objects.filter(batch_id=self.request.pk)

This filter(batch_id=self.request.pk) doesn't work but I want to implement it in the same logic.

How can I achieve this?

Thanks a lot.

You can use self.kwargs["pk"]

class GetFeedItemView(RetrieveAPIView):
    serializer_class = FeedSerializer

    def get_queryset(self):
        return BatchLog.objects.filter()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM