Django 3 在一个模板中结合了 ManyToMany 关系中的 DetailView 和项目(ListView)
Django 3 combine DetailView and items (ListView) from ManyToMany relationship in one template
我正在做一个 Django 3 项目,我试图让 link 人了解他们使用的产品。最终目标是显示此人的详细视图(DetailView)并包括他们使用的产品列表(ListView?)。这是我的模型、视图和模板中的内容:
# models.py (omitting non-relevant fields)
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=75, db_index=True)
products = models.ManyToManyField('Product', through='PersonProduct')
class Product(models.Model):
name = models.CharField("Product Name", max_length=75)
class PersonProduct(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
created = models.DateTimeField(default=timezone.now)
# views.py
class PersonDetailView(DetailView):
model = Person
queryset = Person.objects.all()
template_name = 'person_detail.html'
# person_detail.html (simplified for clarity)
{% extends 'base.html' %}
{% block content %}
<div class="person-detail">
<h2>{{ person.first_name }} {{ person.last_name }}</h2>
</div>
<div class="gear-list">
<ul>
<!--
This is where I'm stuck. I know I need to iterate over the products
that are associated with the person, but I can't figure out how to do it.
-->
</ul>
</div>
{% endblock content %}
该页面可以很好地呈现此人的详细信息,但对于 he/she 使用的产品,那里根本没有任何信息。我已经确认 "PersonProduct" 连接点 table 有我正在测试的特定人员的产品条目。
我知道我对这应该如何工作的理解存在很大差距,但我无法在任何地方找到答案。欢迎就此问题的解决方案和 read/study 的其他资源提出建议。
你可以这样做:
{% for product in person.products.all %}
<li>{{ product.name }}</li>
{% endfor %}
或者,如果您需要 through
table 的日期:
{% for personproduct in person.personproduct_set.all %}
<li>{{ personproduct.products.name }} - {{ personproduct.created }}</li>
{% endfor %}
我正在做一个 Django 3 项目,我试图让 link 人了解他们使用的产品。最终目标是显示此人的详细视图(DetailView)并包括他们使用的产品列表(ListView?)。这是我的模型、视图和模板中的内容:
# models.py (omitting non-relevant fields)
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=75, db_index=True)
products = models.ManyToManyField('Product', through='PersonProduct')
class Product(models.Model):
name = models.CharField("Product Name", max_length=75)
class PersonProduct(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
created = models.DateTimeField(default=timezone.now)
# views.py
class PersonDetailView(DetailView):
model = Person
queryset = Person.objects.all()
template_name = 'person_detail.html'
# person_detail.html (simplified for clarity)
{% extends 'base.html' %}
{% block content %}
<div class="person-detail">
<h2>{{ person.first_name }} {{ person.last_name }}</h2>
</div>
<div class="gear-list">
<ul>
<!--
This is where I'm stuck. I know I need to iterate over the products
that are associated with the person, but I can't figure out how to do it.
-->
</ul>
</div>
{% endblock content %}
该页面可以很好地呈现此人的详细信息,但对于 he/she 使用的产品,那里根本没有任何信息。我已经确认 "PersonProduct" 连接点 table 有我正在测试的特定人员的产品条目。
我知道我对这应该如何工作的理解存在很大差距,但我无法在任何地方找到答案。欢迎就此问题的解决方案和 read/study 的其他资源提出建议。
你可以这样做:
{% for product in person.products.all %}
<li>{{ product.name }}</li>
{% endfor %}
或者,如果您需要 through
table 的日期:
{% for personproduct in person.personproduct_set.all %}
<li>{{ personproduct.products.name }} - {{ personproduct.created }}</li>
{% endfor %}