Suppose that we have 2 models for example, Student and School. We have a filterset for student, for example we can query by birthdate__gt
for example now suppose that we wanna have exact same filters for school but prefixed with student__
so that we can query by student__birthdate__gt
. Is it possible to somehow embed a filterset in another and make it apply to a foreignkey or manytomanyfield?
Filter
'sname
argument accepts the double-underscore to traverse relationships.
https://django-filter.readthedocs.io/en/develop/ref/filters.html#name
This should give you what you need, yes?
@carltongibson unfortunately not. I'm already using double-underscore. The problem is suppose that Student
has 15 filters, currently I have to rewrite all those 15 filters in School
filter set and prefix them with student__
and rewrite all method filters, if I use these 15 filters in 10 other filtersets I should rewrite it 10 times. If something changes in main filterset I should take care of all those 5 filtersets manually. I need to write it once in Student
filterset, and reuse it everywhere else so that I maintain only one code not 6. For my project with lots of cases like this (lots of ForeignKey
s and ManyToManyField
s) it's just a nightmare to handle it manually.
Hmmm. OK. The short answer is, there's nothing built in. But you should be able to do something with a Mixin or such.
I see, so can we consider this issue as a feature request? Something like django_filter.filters.EmbedFilter(SomeFilterSet, prefix='pre', reltaed_filed='the_field')
Hi @sassanh - have you seen django-rest-framework-filters? Even if you're not using DRF, the FilterSet
and RelatedFilter
should still be usable.
Side note - I currently maintain the package and am in the process of refactoring how related filtersets work. I'd recommend reading through the open issues to see if the package works for you in its current state.
Notably, there are issues 99 and 100 and the filtering behavior when spanning multi-valued relationships. The package is changing from the join syntax to a subquery approach, which exhibits more correct behavior.
@rpkilby oh great, I see it has many other nice features too. I can postpone this part of project till version 1.0 is out. Is there any expected time for version 1.0?
No timeline, although I now have some spare time on my hands that can be put towards the project.
The other approach is to construct the FilterSet
dynamically. Take inspiration from modelform_factory
— or similar.
You have a base filter set and then add the additional fields calling type
...
extra_filters = { ... } # Dict of Filters
extended_filterset = type('ExtendedFilterSet', (BaseFilterSet,), extra_filters)
In your case it sounds like you have a lot of repetition so something dynamic may suit but in general I advise a little bit more typing at the declaration stage to save (maintenance) pain later.
thanks @carltongibson in case django-rest-framework-filters wasn't ready at the time when I'm going to implement filters I can use this backup plan.
Most helpful comment
No timeline, although I now have some spare time on my hands that can be put towards the project.