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
をすべてのピザ
- 最も頻繁なトッピングで注文
しかし、私は完全に混乱しています。
このような複雑なクエリを作成する方法を教えてください。
ありがとうございます。作品
Topping.objects.filter(pizza__locations__location='Location Name').annotate(count=Count("id")).order_by('-count')
、歓声: –