Bird
0
0

Consider this DRF viewset snippet:

medium📝 Predict Output Q4 of 15
Django - REST Framework Basics
Consider this DRF viewset snippet:
from rest_framework import viewsets
from .models import Order
from .serializers import OrderSerializer

class OrderViewSet(viewsets.ModelViewSet):
    queryset = Order.objects.filter(status='completed')
    serializer_class = OrderSerializer

What will be the result when accessing this API endpoint?
AThe API returns a list of all completed orders serialized by OrderSerializer
BThe API returns all orders regardless of status
CThe API raises an error due to missing permission classes
DThe API returns an empty list because queryset is not defined properly
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the queryset

    The queryset filters Order objects with status='completed'.
  2. Step 2: Understand ModelViewSet behavior

    ModelViewSet uses the queryset and serializer_class to return serialized data.
  3. Final Answer:

    The API returns a list of all completed orders serialized by OrderSerializer -> Option A
  4. Quick Check:

    Queryset filters data correctly; no error expected without explicit permissions [OK]
Quick Trick: ModelViewSet uses queryset and serializer_class to serve filtered data [OK]
Common Mistakes:
MISTAKES
  • Assuming all orders are returned ignoring queryset filter
  • Expecting errors without explicit permission classes
  • Thinking queryset is undefined or empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes