0
0
Djangoframework~10 mins

Filtering with django-filter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the django-filter package.

Django
import [1]
Drag options to blanks, or click blank then click option'
Adjango_filters
Bdjango-filter
Cfilter_django
Ddjangofilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using a dash instead of underscore in the import.
Trying to import 'filter_django' which does not exist.
2fill in blank
medium

Complete the code to create a filter class for a model named Product.

Django
class ProductFilter([1].FilterSet):
    class Meta:
        model = Product
        fields = ['name', 'category']
Drag options to blanks, or click blank then click option'
Afilters
Bdjango_filter
Cfilter
Ddjango_filters
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filters.FilterSet' instead of 'django_filters.FilterSet'.
Misspelling the package name.
3fill in blank
hard

Fix the error in the view to apply the filter to the queryset.

Django
def product_list(request):
    f = ProductFilter(request.GET, queryset=Product.objects.all())
    return render(request, 'products.html', {'[1]': f})
Drag options to blanks, or click blank then click option'
Afilterset
Bfilter
Cfilters
Dfilterset_qs
Attempts:
3 left
💡 Hint
Common Mistakes
Passing filterset_qs which is not defined here.
Using a wrong context key that the template does not expect.
4fill in blank
hard

Fill both blanks to filter products with price greater than 20 and order by name.

Django
filtered_products = Product.objects.filter(price__[1]=20).order_by([2])
Drag options to blanks, or click blank then click option'
Agt
Blt
C'name'
D'-name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lt' which means less than.
Ordering by '-name' which sorts descending.
5fill in blank
hard

Fill all three blanks to create a filter that filters by category 'Books', price less than 50, and orders by descending price.

Django
filtered = Product.objects.filter(category=[1], price__[2]=[3]).order_by('-price')
Drag options to blanks, or click blank then click option'
A'Books'
Blt
C50
D'Books & More'
Attempts:
3 left
💡 Hint
Common Mistakes
Using category without quotes.
Using 'gt' instead of 'lt' for price.
Using wrong price value.