Bird
0
0

You wrote this FilterSet but the filtering has no effect:

medium📝 Debug Q6 of 15
Django - DRF Advanced Features
You wrote this FilterSet but the filtering has no effect:
class UserFilter(FilterSet):
    class Meta:
        model = User
        fields = ['username', 'email']

filter = UserFilter({'user': 'john'}, queryset=User.objects.all())

What is the issue?
AFilterSet must inherit from django_filters.FilterSet
BMissing import of FilterSet
CUsing 'user' instead of 'username' in filter parameters
DQueryset is not passed correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check filter parameter keys

    The FilterSet fields are 'username' and 'email', but filter uses 'user' which is invalid.
  2. Step 2: Identify the mismatch

    The parameter 'user' does not match any declared field, so django-filter ignores it and returns the unfiltered queryset.
  3. Final Answer:

    Using 'user' instead of 'username' in filter parameters -> Option C
  4. Quick Check:

    Filter keys must match FilterSet fields [OK]
Quick Trick: Filter keys must match FilterSet fields exactly [OK]
Common Mistakes:
MISTAKES
  • Using wrong filter parameter names
  • Forgetting to import FilterSet
  • Not inheriting from correct FilterSet class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes