我尝试选择所有Offers
可用的City
with id=1
,但我得到的结果是 X 倍,其中 X 是Shops
with的数量city_id=1
:
模型.py:
class Cities(models.Model):
name = models.CharField(max_length=128)
slug = models.SlugField(unique=True)
class Shop(models.Model):
name = models.CharField(max_length=128)
city = models.ManyToManyField(to=Cities)
class Product(models.Model):
title = models.CharField(max_length=128)
class Offer(models.Model):
product = models.ForeignKey(to=Product)
shop = models.ManyToManyField(to=Shop)
视图.py:
print Offer.objects.all().filter(shop__city=1)
我想:
[<Offer: Test1>]
但我得到:
[<Offer: Test1>, <Offer: Test1>, <Offer: Test1>]
我的数据库中有 1条Offer
记录和 3Shop
条记录(同一个城市)。
你能告诉我我对多对多关系/请求的理解错误吗?