We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Prefetch objects enable Django developers to control the operation of prefetch_related
. When we pass in a string argument to prefetch related, weβre saying fetch all of the related objects.
A prefetch object lets us pass in a custom queryset to fetch a subset of the related objects.
tickers_with_prefetch = Ticker.objects.all().prefetch_related(
models.Prefetch(
"ticker_prices",
queryset=TickerPrice.objects.filter(
models.Q(close_date=today)
| models.Q(close_date=yesterday)
),
)
)
In this example, we combine a previous query we made for ticker prices from today or yesterday and pass that as the query set of our prefetch object. We fetch all tickers and with them we fetch all related ticker prices from today and yesterday.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.