1
我有以下型號。如何通過外鍵和多對多關係獲取一組值?
class Pizza(models.Model):
toppings = models.ManyToManyField(
Topping,
blank=True,
through='PizzaTopping'
)
locations = models.ManyToManyField(
Location,
blank=True,
through='PizzaLocations'
)
class PizzaTopping(models.Model):
pizza = models.ForeignKey(
Pizza,
on_delete=models.CASCADE
)
topping = models.ForeignKey(
Topping,
on_delete=models.CASCADE
)
class PizzaLocations(models.Model):
pizza = models.ForeignKey(
Pizza,
on_delete=models.CASCADE
)
location = models.ForeignKey(
Location,
on_delete=models.CASCADE
)
class Topping(models.Model):
topping = models.CharField(
max_length=255,
null=True,
blank=True,
db_index=True)
class Location(models.Model):
location = models.CharField(
max_length=255,
null=True,
blank=True,
db_index=True)
我想做到這一點查詢:
Get the most popular toppings where location = some location
我明白我需要做的是這樣的:
- 過濾器的位置
- 獲取所使用的配料的比薩餅所有比薩餅
- 按最常見的配料排序
但是我越來越困惑。
請你幫忙解釋一下如何構建這樣一個複雜的查詢?
謝謝。
這樣的作品,歡呼 –