簡體   English   中英

DjangoRestFramework - 如何使用“pk”過濾列表?

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

我想根據我通過請求發送的pk過濾BatchLog對象列表 - 這是他們的batch_id 有沒有辦法在views.py文件上訪問它?

我的urls.py文件中有這個

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

在我的 views.py 文件中,我想通過過濾它的 batch_id 來訪問BatchLog batch_id

class GetFeedItemView(RetrieveAPIView):
    serializer_class = FeedSerializer

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

filter(batch_id=self.request.pk)不起作用,但我想以相同的邏輯實現它。

我怎樣才能做到這一點?

非常感謝。

你可以使用self.kwargs["pk"]

class GetFeedItemView(RetrieveAPIView):
    serializer_class = FeedSerializer

    def get_queryset(self):
        return BatchLog.objects.filter(batch_id=self.kwargs["pk"])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM