Bird
0
0

Given the following FilterSet and queryset, what will be the result of filtering with author='Alice'?

medium📝 component behavior Q13 of 15
Django - DRF Advanced Features
Given the following FilterSet and queryset, what will be the result of filtering with author='Alice'?
class BookFilter(FilterSet):
    class Meta:
        model = Book
        fields = ['author']

books = Book.objects.all()
filtered_books = BookFilter({'author': 'Alice'}, queryset=books).qs
AA queryset containing only books where the author field is 'Alice'
BA queryset containing all books regardless of author
CAn empty queryset because 'author' is not a valid filter
DA syntax error due to incorrect FilterSet usage
Step-by-Step Solution
Solution:
  1. Step 1: Understand FilterSet filtering

    Providing {'author': 'Alice'} filters the queryset to only include books with author 'Alice'.
  2. Step 2: Confirm no errors in code

    The FilterSet is correctly defined and used, so no syntax or runtime errors occur.
  3. Final Answer:

    A queryset containing only books where the author field is 'Alice' -> Option A
  4. Quick Check:

    FilterSet filters queryset by given field values [OK]
Quick Trick: FilterSet with dict filters queryset by those values [OK]
Common Mistakes:
MISTAKES
  • Assuming it returns all books without filtering
  • Thinking 'author' is invalid filter
  • Confusing FilterSet with form validation errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes