Bird
0
0

You want to create a DRF API endpoint that allows listing all Product items and creating new ones in the same view. Which generic view class should you use and why?

hard📝 Application Q15 of 15
Django - REST Framework Basics
You want to create a DRF API endpoint that allows listing all Product items and creating new ones in the same view. Which generic view class should you use and why?
AUse generics.ListAPIView only because creation should be separate
BUse generics.RetrieveUpdateDestroyAPIView because it handles all CRUD operations
CUse generics.ListCreateAPIView because it supports both listing and creating in one view
DUse generics.CreateAPIView only because listing is not needed
Step-by-Step Solution
Solution:
  1. Step 1: Identify the requirement

    The endpoint must list all products and allow creating new ones in the same view.
  2. Step 2: Match generic view to requirement

    ListCreateAPIView is the correct choice as it supports GET (listing) and POST (creating). Use generics.RetrieveUpdateDestroyAPIView because it handles all CRUD operations (RetrieveUpdateDestroyAPIView) is for single-object detail operations. Options A and C support only a single action each.
  3. Final Answer:

    Use generics.ListCreateAPIView because it supports both listing and creating in one view -> Option C
  4. Quick Check:

    List + Create = ListCreateAPIView [OK]
Quick Trick: ListCreateAPIView combines list and create in one view [OK]
Common Mistakes:
MISTAKES
  • Using RetrieveUpdateDestroyAPIView for list/create
  • Choosing only CreateAPIView or ListAPIView alone
  • Not combining actions in one view

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes