-
Notifications
You must be signed in to change notification settings - Fork 0
Home
amille44420 edited this page Oct 21, 2014
·
2 revisions
First you need to define your fields using Django fields.
from django imports forms
from django_dynamic_filter import Field
class MyField(Field):
class Meta:
field = forms.CharField() # the Django field to use for the exposed form
name = 'relation__field' # optional - if missing the name related to the filter class'll be used
operator = 'icontains' # optional
Now you can define your dynamic filter
from django_dynamic_filter import DynamicFilter
class Myfilter(DynamicFilter):
myfield = MyField()
class Meta:
model = MyModel
Then use it in your view
def myview(request):
filter = MyFilter(request) # just pass it the request instance
objects = filter.render_query()
# you also can pass addtional argument to your filter
# filter.render_query(pk__lts=42)
....
To render the exposed form in your template, you just have to call {{ filter.form }}