suppose this url:
http://localhost:8000/articles/1111/comments/
i'd like to get all comments for a given article (here the 1111).
This is how i capture this url:
url(r'^articles/(?P<uid>[-\w]+)/comments/$', comments_views.CommentList.as_view()),
The related view looks like to:
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
For information, the related serializer
class CommentSerializer(serializers.ModelSerializer):
owner = UserSerializer()
class Meta:
model = Comment
fields = ('id', 'content', 'owner', 'created_at')
As you can see, i've updated my get_queryset to filter comments on article but i don't know how to catch the "uid" parameter. With an url ending with ?uid=value, i can use self.request.QUERY_PARAMS.get('uid') but in my case i don't know how to do. An idea ?
The url parameter is stored in
self.kwargs
.lookup_field
is the field (defaults to pk) the generic view uses inside the ORM when looking up individual model instances,lookup_url_kwarg
is probably the property you want.So try the following: