Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the FilterSet for django-filter located?

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.

like image 990
alphabet5 Avatar asked Mar 20 '23 08:03

alphabet5


1 Answers

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
like image 102
elmonkeylp Avatar answered Apr 08 '23 10:04

elmonkeylp