I have a model in Django like this
class Artist(models.model):
pass
class Track(models.model):
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
I only have a couple hundred Artists that I want to display on a page alongside their latest Track. I can fetch all the Artists quickly, but fetching their latest Track, when there are thousands of tracks, is proving to be expensive.
latest_track = artist.track_set.latest('when') # really expensive
The way I add tracks to an artist allows me to easily set an artists latest track (i.e. I don't have to worry about multiple threads). This made me think I should just store a field Artist#latest_track, but I don't know how to model that because it's not really a OneToOneField, and that's the closest thing I could find.
I also considered using cached_property, but I don't have a good way to invalidate the cache at the right time. Also, cached_property appears to only setup during runtime, so if I restart my app server, it will need to redo all the queries.
What do I do here?
I bet you just need
prefetch_relatedon artists:If it is still slow you need to inspect the generated sql (django debug toolbar, db logs) and see what's going on, often the slowness is coming not from where you expect.