首页 > 解决方案 > 模板中的 Django ForeinKey

问题描述

我正在 django 中建立一个多供应商。我有一个一般的商家表和产品表。如何在产品模板中使用商家店铺名称。

模型.py

class Merchant(models.Model):
    shop_name=models.CharField(max_length=100)
    shop_address=models.CharField(max_length=100)
class Product(models.Model):
    name = models.CharField(max_length=255, unique=True)
    merchant=models.ManyToManyField(Merchant)

详细信息.html

{{产品名称}}

{{product.brand}}

{{product.category__shop_name}}

shop_name 未出现在模板中。预先感谢。

标签: pythonpython-3.xdjangodjango-modelsdjango-templates

解决方案


使用循环

{% for merchant in product.merchant.all %}
    {{ merchant.shop_name }}
{% endfor %}

推荐阅读