Bird
0
0

Which of the following is the correct way to use a generic view to list all objects of a model named Book in DRF?

easy📝 Syntax Q12 of 15
Django - REST Framework Basics
Which of the following is the correct way to use a generic view to list all objects of a model named Book in DRF?
Aclass BookList(generics.ListAPIView):\n queryset = Book.objects.all()\n serializer_class = BookSerializer
Bclass BookList(generics.CreateAPIView):\n queryset = Book.objects.all()\n serializer_class = BookSerializer
Cclass BookList(generics.UpdateAPIView):\n queryset = Book.objects.all()\n serializer_class = BookSerializer
Dclass BookList(generics.DestroyAPIView):\n queryset = Book.objects.all()\n serializer_class = BookSerializer
Step-by-Step Solution
Solution:
  1. Step 1: Identify the generic view for listing objects

    The ListAPIView is designed to list all objects of a model.
  2. Step 2: Match the class with the task

    Options B, C, and D correspond to creating, updating, and deleting respectively, which do not list objects.
  3. Final Answer:

    class BookList(generics.ListAPIView):\n queryset = Book.objects.all()\n serializer_class = BookSerializer -> Option A
  4. Quick Check:

    List all objects = ListAPIView = A [OK]
Quick Trick: List objects use ListAPIView class [OK]
Common Mistakes:
MISTAKES
  • Using CreateAPIView for listing data
  • Confusing UpdateAPIView with ListAPIView
  • Forgetting to set queryset or serializer_class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes