[英]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
.我想根据我通过请求发送的pk
过滤BatchLog
对象列表 - 这是他们的batch_id
。 Is there a way to access it on views.py
file?有没有办法在views.py
文件上访问它?
I have this in my urls.py
file我的urls.py
文件中有这个
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
在我的 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)
This filter(batch_id=self.request.pk)
doesn't work but I want to implement it in the same logic.此filter(batch_id=self.request.pk)
不起作用,但我想以相同的逻辑实现它。
How can I achieve this?我怎样才能做到这一点?
Thanks a lot.非常感谢。
You can use self.kwargs["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.