python - 模板中的 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 未出现在模板中。预先感谢。
解决方案
使用循环
{% for merchant in product.merchant.all %}
{{ merchant.shop_name }}
{% endfor %}
推荐阅读
- json - Flutter如何获取一个json的数据
- javascript - chrome 扩展,将数据传递给 iframe
- python - 使用 Python 中的列名列表在 MariaDB 中创建表
- r - 操作嵌套数据
- arrays - 如何访问数组中的第一个用户
- android - ESP8266 与安卓通信
- excel - 如果公司销售两种特定产品,则显示公司
- python - how to load images data into pytorch dataLoader?
- c# - 使用 C# ASP.Net 在 SQL Server 中重复条目
- javascript - passing vars from js to node