Fetch child data with parents in Django queries

后端 未结 2 1648
面向向阳花
面向向阳花 2021-01-29 04:28

I have two Model Product and ProductBundle. ProductBundle have a foreign key of Product Model. How can I access a list of all product with there productbundle.

c         


        
2条回答
  •  走了就别回头了
    2021-01-29 04:46

    You should change the related_name of product inside ProductBundle to something like bundles and then you can go product.bundles.all() to access all ProductBundle related to a Product

    edit to filter all products if they have a ProductBundle

    assuming you are using bundles as related_name: Product.objects.filter(bundles__isnull=False).distinct()

提交回复
热议问题