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

Is there anyway to do a select_related() to prevent additional database hit #75

Open
ericel opened this issue Jul 26, 2021 · 1 comment

Comments

@ericel
Copy link

ericel commented Jul 26, 2021

I want to get the voters of an instance. That way I will check in the frontend if has already voted and displayed a UI according to that status.
But getting the review.votes.user_ids() results in additional database hit.
How to get this value without some additional database hits?

I tweaked my app, for now, to use prefetch_related('votes'). But this will still result in a 1-time additional database hit.
I want to avoid that altogether if possible.

@shellfly
Copy link
Owner

You can use the annotate method, pass in a query set and the current user id, it will return a new query set with is_voted_up and is_voted_down property set on the records you are fetching.
It's still an additional database hit but it's only one hit per query set, not per instance.

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