0
0
Djangoframework~20 mins

ViewSets and routers in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ViewSets and Routers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this ViewSet router URL pattern?
Given the following Django REST Framework router and ViewSet, what URL pattern will be registered for the 'list' action?
Django
from rest_framework import routers, viewsets
from django.urls import path, include

class BookViewSet(viewsets.ViewSet):
    def list(self, request):
        pass

router = routers.DefaultRouter()
router.register(r'books', BookViewSet, basename='book')

urlpatterns = [
    path('', include(router.urls)),
]
A/book/list/ mapped to BookViewSet list action
B/book/ mapped to BookViewSet list action
C/books/ mapped to BookViewSet list action
D/books/list/ mapped to BookViewSet list action
Attempts:
2 left
💡 Hint
Remember how DefaultRouter registers routes for ViewSets using the basename and prefix.
state_output
intermediate
1:30remaining
What is the HTTP method for the 'create' action in a ViewSet?
In Django REST Framework, which HTTP method triggers the 'create' action in a ViewSet when using a router?
AGET
BPOST
CPUT
DDELETE
Attempts:
2 left
💡 Hint
Think about which HTTP method is used to add new data.
📝 Syntax
advanced
2:30remaining
Which option correctly registers a ViewSet with a router for nested routes?
You want to register a nested route for comments under posts using routers in Django REST Framework. Which code snippet correctly does this?
A
router.register(r'posts', PostViewSet)
nested_router = routers.NestedDefaultRouter(router, r'posts', lookup='post')
nested_router.register(r'comments', CommentViewSet, basename='post-comments')
Brouter.register(r'posts/(?P<post_pk>[^/.]+)/comments', CommentViewSet, basename='post-comments')
C
router.register(r'posts', PostViewSet)
router.register(r'posts/comments', CommentViewSet)
D
router.register(r'posts', PostViewSet)
router.register(r'comments', CommentViewSet)
Attempts:
2 left
💡 Hint
Nested routes require a special nested router, not just string patterns.
🔧 Debug
advanced
2:00remaining
Why does this ViewSet raise a 405 Method Not Allowed error?
Consider this ViewSet: class ArticleViewSet(viewsets.ViewSet): def retrieve(self, request, pk=None): return Response({'id': pk}) router.register(r'articles', ArticleViewSet) Why does a GET request to /articles/ return 405 Method Not Allowed?
ABecause the basename is missing in router.register
BBecause 'retrieve' method is missing the 'self' parameter
CBecause the router is not included in urlpatterns
DBecause 'list' method is not defined, so GET on /articles/ is not allowed
Attempts:
2 left
💡 Hint
Check which actions correspond to which URLs and HTTP methods.
🧠 Conceptual
expert
3:00remaining
What is the main advantage of using routers with ViewSets in Django REST Framework?
Why do developers prefer using routers with ViewSets instead of manually defining URL patterns for each view?
ARouters automatically generate standard RESTful URL patterns and reduce repetitive code
BRouters improve database query performance by caching queries
CRouters allow using class-based views without serializers
DRouters enable asynchronous request handling by default
Attempts:
2 left
💡 Hint
Think about how routers help with URL management and code simplicity.