【发布时间】:2015-11-03 00:40:11
【问题描述】:
我有 django 1.8.5 和 Python 3.4.3,并尝试创建一个约束我的主数据集的子查询 - 但子查询本身(我认为)需要加入其中。或者也许有更好的方法。
这是一组精简的模型:
class Lot(models.Model):
lot_id = models.CharField(max_length=200, unique=True)
class Lot_Country(models.Model):
lot = models.ForeignKey(Lot)
country = CountryField()
class Discrete(models.Model):
discrete_id = models.CharField(max_length=200, unique=True)
master_id = models.ForeignKey(Inventory_Master)
location = models.ForeignKey(Location)
lot = models.ForeignKey(Lot)
我正在过滤 Discrete 的各种属性(即离散供应),并且我想通过 Lot,在 Lot_Country 上“向上”,这意味着“如果与该行关联的 Lot 有,我只想从 Discrete 获取行在 Lot_Country 中为我的相应国家/地区(比如美国)输入。
我尝试过这样的事情:
oklots=list(Lot_Country.objects.filter(country='US'))
但是,首先,这让我得到了 str,这是我并不真正想要的(并将其更改为 lot_id,但这是一个 hack。)
限制离散通过 Lot 到 Lot_Country 的最佳方法是什么?在 SQL 中,我将只加入子查询(甚至在主查询中 - 也许这就是我需要的?我想我不知道如何加入父母然后再加入该父母的另一个孩子......)
提前感谢您的帮助。
【问题讨论】: