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

Extract form validation and queryset filtering from caching #783

Closed
wants to merge 1 commit into from

Conversation

rpkilby
Copy link
Collaborator

@rpkilby rpkilby commented Sep 24, 2017

I was thinking about #740 a while back, and realized that overriding the FilterSet.qs property is slightly awkward, due to the _qs caching. In the case of #740, Prefetch calls can only occur once, as duplicate calls can raise an exception. If you want to cache the prefetch, a correct solution looks like the following:

@property
def qs(self):
    # ensure that we haven't already cached, as this also implies prefetching
    already_prefetched = hasattr(self, '_qs')
    super().qs
    if self.is_bound and not already_prefetched:
        self._qs = self._qs.prefetch_related(...)
    return self._qs

Overriding filter_queyset() is a little more intuitive, as it would only be executed once.

def filter_queryset(self, queryset):
    qs = super().filter_queryset(queryset)
    return qs.prefetch_related(...)

@codecov-io
Copy link

codecov-io commented Sep 24, 2017

Codecov Report

Merging #783 into develop will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #783      +/-   ##
===========================================
- Coverage    98.36%   98.35%   -0.01%     
===========================================
  Files           15       15              
  Lines         1220     1219       -1     
===========================================
- Hits          1200     1199       -1     
  Misses          20       20
Impacted Files Coverage Δ
django_filters/filterset.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 978e9ef...09bc364. Read the comment docs.

@rpkilby
Copy link
Collaborator Author

rpkilby commented Oct 12, 2017

Temporarily closing in favor of alternative approach.

@rpkilby rpkilby closed this Oct 12, 2017
@rpkilby rpkilby deleted the extract-qs-filtering branch October 20, 2017 17:39
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

Successfully merging this pull request may close these issues.

None yet

2 participants