Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django查询中获取父级的子数据_Python_Django_Django Models_Django Views - Fatal编程技术网

Python 在Django查询中获取父级的子数据

Python 在Django查询中获取父级的子数据,python,django,django-models,django-views,Python,Django,Django Models,Django Views,我有两个模型产品和产品包。ProductBundle具有产品模型的外键。如何访问带有productbundle的所有产品的列表 class Product(models.Model): title = models.CharField(verbose_name="Product Title", max_length=500) class ProductBundle(models.Model): product = models.ForeignKey(Product,on_del

我有两个模型产品和产品包。ProductBundle具有产品模型的外键。如何访问带有productbundle的所有产品的列表

class Product(models.Model):
    title = models.CharField(verbose_name="Product Title", max_length=500)

class ProductBundle(models.Model):
    product = models.ForeignKey(Product,on_delete=models.CASCADE,related_name="product")
    bundle_product = models.ForeignKey(Product,on_delete=models.CASCADE,related_name="bundle_product",default="")
    quantity = models.IntegerField(default="1")
我想获取所有有productbundle ID的产品,如 在单个变量中为childs的父项


谢谢。

您应该将
ProductBundle
内部
ProductBundle
相关名称更改为类似
bundles
的内容,然后您可以转到
product.bundles.all()
访问与
产品相关的所有
ProductBundle

编辑以筛选所有产品(如果它们有
ProductBundle

假设您将
捆绑包
用作
相关的\u名称

Product.objects.filter(bundles\uu isnull=False).distinct()

您应该将
Product
内部
ProductBundle
相关名称
更改为类似
bundles
的内容,然后您可以转到
Product.bundles.all()
访问与
产品相关的所有
ProductBundle

编辑以筛选所有产品(如果它们有
ProductBundle

假设您将
捆绑包
用作
相关的\u名称

Product.objects.filter(bundles\uu isnull=False).distinct()

对于每个
产品
,您已经有了
ProductBundle
对象作为

假设您执行
products=Product.objects.all()
,您可以通过执行以下操作来访问每个产品的捆绑包:

for product in products:
    product_bundles = product.productbundle_set.all()
根据评论进行编辑:

如果您想在模板中显示所有产品及其捆绑包,您可以做几乎相同的事情。在
视图中
获取变量中的所有产品,例如
products=Product.objects.all()
并将其传递给模板。假设变量名为
products
,则可以执行以下操作:

{% for product in products %}
    <h1>{{product.title}}</h1>
    <h1>Bundles:</h1>
    {% for bundle in product.productbundle_set.all %}
        <h2>{{bundle.quantity}}</h2>
    {% endfor %}
{% endfor %}
{%用于产品中的产品%}
{{product.title}}
捆:
{product.productbundle_set.all%}
{{bundle.quantity}}
{%endfor%}
{%endfor%}

对于每个
产品
,您已经有了
ProductBundle
对象作为

假设您执行
products=Product.objects.all()
,您可以通过执行以下操作来访问每个产品的捆绑包:

for product in products:
    product_bundles = product.productbundle_set.all()
根据评论进行编辑:

如果您想在模板中显示所有产品及其捆绑包,您可以做几乎相同的事情。在
视图中
获取变量中的所有产品,例如
products=Product.objects.all()
并将其传递给模板。假设变量名为
products
,则可以执行以下操作:

{% for product in products %}
    <h1>{{product.title}}</h1>
    <h1>Bundles:</h1>
    {% for bundle in product.productbundle_set.all %}
        <h2>{{bundle.quantity}}</h2>
    {% endfor %}
{% endfor %}
{%用于产品中的产品%}
{{product.title}}
捆:
{product.productbundle_set.all%}
{{bundle.quantity}}
{%endfor%}
{%endfor%}

我想在模板中使用该产品来显示所有产品,然后再显示productbundle。我该怎么做?已更新模板用法。感谢更新,但此查询不起作用,因为我有来自产品的两个外键。我现在如何访问它?我想在模板中使用该产品来显示所有产品,然后再显示productbundle。我该怎么做?已更新模板用法。感谢更新,但此查询不起作用,因为我有来自产品的两个外键。如果我在SQL中考虑这个问题,那么如果我与产品和产品捆绑在一起,我就可以得到产品捆绑的产品列表。我如何在Django中做到这一点?你的意思是找到至少有一个productbundle的所有产品吗?所有产品如果它们有productbundle,那么这些产品也可以包括该productbundle。在产品和产品BayLee中SQL的左边加入,如果我在SQL中考虑这个问题,那么我猜你正在寻找答案,所以如果我加入产品和产品捆绑,我可以得到产品捆绑的产品列表。我如何在Django中做到这一点?你的意思是找到至少有一个productbundle的所有产品吗?所有产品如果它们有productbundle,那么这些产品也可以包括该productbundle。就像在product和ProductBundle之间的sql中的左连接一样,我用我猜你在问的问题编辑了答案