The documentation shows
We have a number of fields and we want to let our users filter based on the price or the release_date. We create a FilterSet for this:
import django_filters
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = ['price', 'release_date']
Where does this code get placed to create a FilterSet? Is it in models or views? Thanks.
Wherever you want, I mean models.py, views.py or even a new file called filters.py. Because you will use that filter in views.py, so you can import the filters from everywhere in your project. For me, I think a file filters.py in the app is the best place.
So in your views.py import the filters like this:
from .filters import ProductFilter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With