为什么Django QuerySet不支持manyTomany联接?

时间:2019-06-02 06:37:20

标签: python django orm many-to-many django-queryset

如何生成代码段1-2之类的SQL?


为什么Django仅支持prefetch_related('product_set')而不支持与manytomanyField联接?

我当然可以进行类似的查询集

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;

0 个答案:

没有答案