python - django- Use prefetch_related inside of another prefetch_related -


closest thing asking can found here

say have following models:

class division(models.model):     name = models.textfield()     state = models.integerfield()  class team(models.model):     name2 = models.textfield()     division = models.foreignkey(division, ...)  class player(models.model):     name = models.textfield()     hometown = models.integerfield()     team = models.foreignkey(team, ...) 

now can following 1 table:

players = player.objects.prefetch_related('team') 

how go adding state queryset? endgoal able player.team.division.state inside of template. other alternative use nested loops avoid that.

you don't need prefetch_related here. can follow foreign keys player team division using select_related().

players = player.objects.select_related('team__division') 

a use-case prefetch_related if started division queryset, , wanted fetch related teams @ same time.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -