Complete the code to import the django-filter package.
import [1]
The correct import for django-filter is django_filters. This is the package name used in Python code.
Complete the code to create a filter class for a model named Product.
class ProductFilter([1].FilterSet): class Meta: model = Product fields = ['name', 'category']
The filter class should inherit from django_filters.FilterSet to work properly.
Fix the error in the view to apply the filter to the queryset.
def product_list(request): f = ProductFilter(request.GET, queryset=Product.objects.all()) return render(request, 'products.html', {'[1]': f})
filterset_qs which is not defined here.The context key should be filter or any name you choose, but commonly filter is used to pass the filter object to the template.
Fill both blanks to filter products with price greater than 20 and order by name.
filtered_products = Product.objects.filter(price__[1]=20).order_by([2])
Use gt to filter prices greater than 20 and order by 'name' ascending.
Fill all three blanks to create a filter that filters by category 'Books', price less than 50, and orders by descending price.
filtered = Product.objects.filter(category=[1], price__[2]=[3]).order_by('-price')
Filter category by the string 'Books', price less than 50 using 'lt', and order descending by price.