如何生成代码段1-2之类的SQL?
我当然可以进行类似的查询集
Order.objects.filter(product_set_included_order__name ='product_name1')
它通过连接两个实体生成SQL
但是它有一个大问题,没有达到索引(pk)
# 1-1 this is snippet code
# in Django model of relationship with ManyToMany
class Product(models.Model):
name: str = models.CharField(null=False, max_length=128)
price: int = models.PositiveIntegerField(null=False, default=0)
class Order(models.Model):
product_set= models.ManyToManyField(to=Product)
# 1-2 i want generate this SQL but it's impossible!!
SELECT *
FROM "order"
INNER JOIN "orderedproduct"
ON ("order"."id" = "orderedproduct"."related_order_id")
INNER JOIN "product"
ON ("orderedproduct"."related_product_id" = "product"."id")
WHERE "order"."id" = 1;