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

Get queryset from RelatedFilter after it was applied? #213

Closed
RobinRamael opened this issue Jan 10, 2018 · 3 comments
Closed

Get queryset from RelatedFilter after it was applied? #213

RobinRamael opened this issue Jan 10, 2018 · 3 comments

Comments

@RobinRamael
Copy link

RobinRamael commented Jan 10, 2018

Is it possible to get the filtered queryset from a related filter?

Say I have models:

class A(models.Model):
    field = models.CharField()


class B(models.Model):
    a = models.ForeignKey(A)

and set up filtering for them

class AFilter(filters.FilterSet):
    class Meta:
        fields = ('field',)


class BFilter(filters.FilterSet):
    a = filters.RelatedFilter(AFilter, queryset=A.objects.all())

    class Meta:
        fields = ('a',)

And use these in a view:

b_qs = BFilter({'a__field': 'foo'}).qs

I was wondering wether there was anyway to get the A queryset from the BFilter as if I was doing

a_qs = AFilter({'field': 'foo'}).qs

without having to resort to removing 'a__' from all keys in the query param dict.

@rpkilby
Copy link
Collaborator

rpkilby commented Jan 10, 2018

The short answer is no, not currently. However, you may be interested in #199, which creates some new API surface, and reworks how related filtering is performed. eg, once #199 is cleaned up and merged, you'd be able to access the related queryset like so:

BFilter({'a__field': 'foo'}).related_filtersets['a']

@rpkilby
Copy link
Collaborator

rpkilby commented Jan 10, 2018

Closing this, as there isn't anything actionable. #199 still needs some polishing, but it will cover the described use case and generally seems like the path forward.

@rpkilby rpkilby closed this as completed Jan 10, 2018
@RobinRamael
Copy link
Author

Thanks!

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

2 participants