【问题标题】:Django query across many-to-many relationship跨多对多关系的 Django 查询
【发布时间】:2011-06-03 20:16:27
【问题描述】:

我正在尝试在视图中检索以下数据:

user_profileusers 具有一对一连接
user_profile 通过stores 字段与store 具有多对多连接
store 具有通过targets.store 一对多加入targets

我可以通过以下方式检索与用户关联的所有商店:

user_profile = request.user.get_profile()
all_stores = user_profile.stores.all()

我要检索的是与user_profile 的用户 ID 关联的所有目标,大概是通过all_stores 结果,但我不知道如何到达那里。

【问题讨论】:

标签: python django django-queryset


【解决方案1】:
Target.objects.filter( store__user_profile__user=request.user )

【讨论】:

  • 谢谢。试图向后/向前跟踪关系,而不是考虑 SQL 连接,我感到困惑。
猜你喜欢
  • 2015-10-26
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 2022-01-15
  • 2014-07-14
  • 2021-03-07
  • 2019-05-18
  • 2017-02-02
相关资源
最近更新 更多