Bird
0
0

Which of the following is the correct way to define a FilterSet class for a model named Book with a filter on the author field?

easy📝 Syntax Q12 of 15
Django - DRF Advanced Features
Which of the following is the correct way to define a FilterSet class for a model named Book with a filter on the author field?
Aclass BookFilter(FilterSet):\n class Meta:\n model = Book\n fields = ['author']
Bclass BookFilter(FilterSet):\n model = Book\n fields = ['author']
Cclass BookFilter(FilterSet):\n class Meta:\n fields = ['author']
Dclass BookFilter(FilterSet):\n class Meta:\n model = Book\n filter_fields = ['author']
Step-by-Step Solution
Solution:
  1. Step 1: Recall FilterSet Meta class structure

    The Meta class must specify the model and the fields list for filtering.
  2. Step 2: Check each option

    class BookFilter(FilterSet):\n class Meta:\n model = Book\n fields = ['author'] correctly defines Meta with model and fields. class BookFilter(FilterSet):\n model = Book\n fields = ['author'] misses Meta class. class BookFilter(FilterSet):\n class Meta:\n fields = ['author'] misses model. class BookFilter(FilterSet):\n class Meta:\n model = Book\n filter_fields = ['author'] uses incorrect attribute 'filter_fields'.
  3. Final Answer:

    class BookFilter(FilterSet):\n class Meta:\n model = Book\n fields = ['author'] -> Option A
  4. Quick Check:

    FilterSet Meta needs model and fields [OK]
Quick Trick: FilterSet Meta needs model and fields list [OK]
Common Mistakes:
MISTAKES
  • Omitting the Meta class
  • Using 'filter_fields' instead of 'fields'
  • Not specifying the model in Meta

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes