0
0
Djangoframework~20 mins

Generic views in DRF in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DRF Generic Views 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 DRF generic view code?

Consider this Django REST Framework generic view:

from rest_framework import generics
from myapp.models import Book
from myapp.serializers import BookSerializer

class BookList(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

What HTTP methods does this view support by default?

APUT and DELETE
BGET and POST
CPOST only
DGET only
Attempts:
2 left
💡 Hint

Think about what ListCreateAPIView is designed for.

📝 Syntax
intermediate
2:00remaining
Which option correctly overrides the get_queryset method in a DRF generic view?

You want to filter the queryset based on the current user in a generic view. Which code snippet is correct?

Django
from rest_framework import generics
from myapp.models import Article
from myapp.serializers import ArticleSerializer

class UserArticles(generics.ListAPIView):
    serializer_class = ArticleSerializer

    def get_queryset(self):
        # Your code here
        pass
Areturn Article.objects.filter(author=self.request.user)
Breturn Article.objects.filter(author=request.user)
Creturn Article.objects.filter(author=user)
Dreturn Article.objects.filter(author=self.user)
Attempts:
2 left
💡 Hint

Remember how to access the request object inside a view method.

state_output
advanced
2:00remaining
What is the response status code when creating an object with CreateAPIView?

Using Django REST Framework's CreateAPIView, what is the typical HTTP status code returned after successfully creating an object?

A204 No Content
B200 OK
C400 Bad Request
D201 Created
Attempts:
2 left
💡 Hint

Think about the standard HTTP status code for resource creation.

🔧 Debug
advanced
2:00remaining
Why does this DRF generic view raise an AttributeError?

Given this view code:

from rest_framework import generics
from myapp.models import Product
from myapp.serializers import ProductSerializer

class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = ProductSerializer

    def get_object(self):
        return Product.objects.get(pk=self.kwargs['id'])

When accessing this view, it raises KeyError: 'id'. Why?

AThe view should use self.get_object() without accessing kwargs directly
BThe view should use self.kwargs from the method parameter, not self.kwargs attribute
CThe view should use self.kwargs, but it is missing because URL pattern does not pass 'id'
DThe view should use self.request instead of self.kwargs
Attempts:
2 left
💡 Hint

Check how URL parameters are passed to the view.

🧠 Conceptual
expert
3:00remaining
Which generic view class should you use to implement a read-only endpoint that returns a list and detail of objects?

You want to create an API endpoint that allows users to only read data (no create, update, or delete). It should support listing all objects and retrieving a single object by ID. Which DRF generic view class is the best choice?

AReadOnlyModelViewSet
BListCreateAPIView
CRetrieveUpdateDestroyAPIView
DListAPIView combined with RetrieveAPIView
Attempts:
2 left
💡 Hint

Think about a class that supports both list and detail read-only actions in one place.