django - Chaining queries through related objects -


given scenario like:

from django.db import models  class player(models.model):     playername = models.charfield()  class team(models):     teamname = models.charfield()  class members(models):     player = models.foreignkey(player)     team = models.foreignkey(team)  class division(models):     divname = models.charfield()    class divisionteam(models):     division = models.foreignkey(division)     team = models.foreignkey(team) 

how can list distinct players in division id = 5? i've through q , f expressions, i'm not looking complex set of or's. wondering if there way chain number of object1_set.object2_set.all() type structures, or set nested loops build object (to passed template via context) eventual {% p in players %} type loop in template. div id passed through request variable.

you do:

players = player.objects.filter(members__team__divisionteam__division_id=5).distinct() 

of course, suggested in other answer, models simplified (by using manytomany rather explicitly managing it)


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -