Skip to content Skip to sidebar Skip to footer

Getting The Many-to-many Fields Of A Many-to-many Object

If I have the user-group relation as a ManyToMany relationship, and a group-team relation as a ManyToMany relationship, how would I find the user-team relation as a query object? F

Solution 1:

Team.objects.filter(groups__user__username="Ed")

which generates you:

SELECT
  `api_team`.`id`,
  `api_team`.`teamname`
FROM `api_team`
  INNER JOIN `api_team_groups` ON (`api_team`.`id` = `api_team_groups`.`team_id`)
  INNER JOIN `api_group` ON (`api_team_groups`.`group_id` = `api_group`.`id`)
  INNER JOIN `api_user_groups` ON (`api_group`.`id` = `api_user_groups`.`group_id`)
  INNER JOIN `api_user` ON (`api_user_groups`.`user_id` = `api_user`.`id`)
WHERE `api_user`.`username` = 'ed'

Post a Comment for "Getting The Many-to-many Fields Of A Many-to-many Object"