我正在尝试从另一个应用程序获取模型ManyToManyField并创建一个列表,但尝试在模板中调用它时会返回“ app1.Model.None”。
app1.models
class Illness(models.Model):
title = models.CharField(max_length=255)
app2.models
class Client(models.Model):
illnesses = models.ManyToManyField('app1.Illness', blank=True)
client.html
<p>{{ client.illnesses }}</p>
app2.views.py
class TheDetailView(generic.DetailView):
template_name = 'client.html'
model = Client
HTML呈现对于该部分仅返回“ app1.Illness.None”。我知道我会在{%for%}循环中列出它们,但是当我尝试将其保留为空白时。我究竟做错了什么?感谢您提前提供的所有帮助。
A manager is repented as
ModelName.None
, for exampleClient.objects
will also be presented asClient.None
.In order to access the items in a
ManyToManyField
relation, you need to access the queryset, for example with.all()
, and iterate over it, if you want to display individual elements, so:或者,如果您想呈现标题: