Bird
0
0

Given this ViewSet and router code, what URL pattern will be created for retrieving a single book by ID?

medium📝 component behavior Q4 of 15
Django - REST Framework Basics
Given this ViewSet and router code, what URL pattern will be created for retrieving a single book by ID?
from rest_framework import routers, viewsets
from myapp.models import Book
from myapp.serializers import BookSerializer

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

router = routers.SimpleRouter()
router.register(r'books', BookViewSet)
A/books/<pk>/
B/books/detail/<pk>/
C/book/<pk>/
D/books/get/<pk>/
Step-by-Step Solution
Solution:
  1. Step 1: Understand SimpleRouter URL patterns

    SimpleRouter creates standard RESTful URLs like /prefix/ and /prefix//.
  2. Step 2: Identify the prefix and action

    Here, prefix is 'books', so retrieve URL is /books//.
  3. Final Answer:

    /books/<pk>/ -> Option A
  4. Quick Check:

    Retrieve URL = /books/<pk>/ [OK]
Quick Trick: Retrieve URLs use /prefix// pattern [OK]
Common Mistakes:
MISTAKES
  • Adding extra path segments like 'detail' or 'get'
  • Using singular 'book' instead of plural 'books'
  • Confusing list and detail URL patterns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes