假设URL为:
http://localhost:8000/articles/1111/comments/
我想知道某篇文章(这里是1111)的所有 comments .
下面是我捕获此url的方式:
url(r'^articles/(?P<uid>[-\w]+)/comments/$', comments_views.CommentList.as_view()),
相关视图如下所示:
class CommentList(generics.ListAPIView):
serializer_class = CommentSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
lookup_field = "uid"
def get_queryset(self):
comments = Comment.objects.filter(article= ???)
return comments
有关详细信息,请参阅相关的序列化程序
class CommentSerializer(serializers.ModelSerializer):
owner = UserSerializer()
class Meta:
model = Comment
fields = ('id', 'content', 'owner', 'created_at')
如您所见,我已经将get_queryset更新为对文章的过滤 comments ,但是我不知道如何捕捉"uid"参数. 如果url以?uid=value结尾,我可以使用self.request.QUERY_PARAMS.get(‘uid’),但在我的例子中,我不知道怎么做. 有什么 idea 吗?