hi! I have a view which loops over a list of battles and gets the battle.player of each battle, which looks like this
@property
def player(self):
return Player.objects.filter(team__battle=self, is_self=True) \
.select_related('title_adjective__string') \
.select_related('title_subject__string') \
.select_related('nameplate_background__image') \
.select_related('nameplate_badge_1__image') \
.select_related('nameplate_badge_2__image') \
.select_related('nameplate_badge_3__image') \
.select_related('weapon__name') \
.select_related('weapon__flat_image') \
.select_related('weapon__sub__name') \
.select_related('weapon__sub__overlay_image') \
.select_related('weapon__sub__mask_image') \
.select_related('weapon__special__name') \
.select_related('weapon__special__overlay_image') \
.select_related('weapon__special__mask_image') \
.get()
This has lead to an n+1 issue but I'm not sure how to solve it. Adding a prefetch_related to the initial query to get the lists of battles with something like prefetch_related('teams__players__<each of the fields above>') seems to just make the problem even worse. Is there a way to optimize this?