Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-rest-framework How to handle multiple URL parameter?

How can I use generic views with multiple URL parameters? Like

GET /author/{author_id}/book/{book_id}

class Book(generics.RetrieveAPIView):

    queryset = Book.objects.all()
    serializer_class = BookSerializer
    lookup_field = 'book_id'
    lookup_url_kwarg = 'book_id'

    # lookup_field = 'author_id' for author
    # lookup_url_kwarg = 'author_id'
like image 624
Sam R. Avatar asked Mar 09 '15 22:03

Sam R.


1 Answers

Might be late to the party here, but this is what I do:

class Book(generics.RetrieveAPIView):

    serializer_class = BookSerializer
    
    def get_queryset(self):
           book_id = self.kwargs['book_id']
           author_id = self.kwargs['author_id']     
           
           return Book.objects.filter(Book = book_id, Author = author_id)
like image 108
Hank Avatar answered Nov 15 '22 04:11

Hank