I have a model that has a dynamic property like:
# field definitions here
user_currency = self.user.currency
fund_currency = self.fund.currency
return self.fund.position * self.fund.price * <currency conversion here>
The information about conversion rates is stored in another table
How to do this calculation without making a lot of extra queries?
Is it OK to query the whole Currency table once and use that as a lookup table inside this user_currency_value function? Will this be cached somehow? Is there a better way to do this?
The main control technique here is with
select_related on Currency, wherever you query
You detect when you need such a call with this library: https://github.com/jmcarp/nplusone
If you’re feeling adventurous you can try this library, which I’m developing with my ex-boss: https://github.com/tolomea/django-auto-prefetch . It changes the N+1 behaviour down to 2 queries instead, by automatically using
prefetch_related on access. We hope to get it into Django core.
For my understanding (I’m new to Django), in the answer here: https://stackoverflow.com/a/28598539/562267, would you apply prefetch_related in the
prefetch_related() there would work. It could end up with overfetching though, as it will always prefetch, even on code paths that don’t touch your property.