![](/img/trans.png)
[英]how can i filter the Json response by only passing the Year parameter to the DateTime field using DateFilter in Djangorestframework
[英]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.