Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to embed a filterset in another #587

Closed
sassanh opened this issue Dec 21, 2016 · 10 comments
Closed

Possibility to embed a filterset in another #587

sassanh opened this issue Dec 21, 2016 · 10 comments

Comments

@sassanh
Copy link

sassanh commented Dec 21, 2016

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?

@carltongibson
Copy link
Owner

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?

@sassanh
Copy link
Author

sassanh commented Dec 21, 2016

@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 ForeignKeys and ManyToManyFields) it's just a nightmare to handle it manually.

@sassanh sassanh changed the title Possibility to embed a filter in another Possibility to embed a filterسثف in another Dec 21, 2016
@sassanh sassanh changed the title Possibility to embed a filterسثف in another Possibility to embed a filterset in another Dec 21, 2016
@carltongibson
Copy link
Owner

Hmmm. OK. The short answer is, there's nothing built in. But you should be able to do something with a Mixin or such.

@sassanh
Copy link
Author

sassanh commented Dec 21, 2016

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')

@rpkilby
Copy link
Collaborator

rpkilby commented Dec 21, 2016

Hi @sassanh - have you seen django-rest-framework-filters? Even if you're not using DRF, the FilterSet and RelatedFiltershould still be usable.

@rpkilby
Copy link
Collaborator

rpkilby commented Dec 21, 2016

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.

@sassanh
Copy link
Author

sassanh commented Dec 22, 2016

@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?

@rpkilby
Copy link
Collaborator

rpkilby commented Dec 22, 2016

No timeline, although I now have some spare time on my hands that can be put towards the project.

@carltongibson
Copy link
Owner

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.

@sassanh
Copy link
Author

sassanh commented Dec 22, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants