Django 在模板中显示多对多关系

Django 在模板中显示多对多关系,django,django-models,django-templates,django-views,django-orm,Django,Django Models,Django Templates,Django Views,Django Orm,首先,我将发布所有可能需要的代码我的型号: class topic(models.Model): learningObjectivesTopic = models.ManyToManyField(learningObjective, verbose_name = "Lernziel") topic = models.TextField(verbose_name = 'Thema') class learningO

首先,我将发布所有可能需要的代码我的型号

class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField(learningObjective, verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')                         

class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')
我的观点:

@login_required(login_url='login')
def lernziel(request):
        return render(request, 'lernziel.html', {'topic': topic.objects.all(), 'todo': todoList.objects.all()})

@login_required(login_url='login')
def create_lernziel(request):
        neuesLernziel=learningObjective(learningObjectives=request.POST['Lernziel'])
        neuesLernziel.save()
        neuesLernziel_Topic=topic.objects.get(topic=request.POST['Thema'])
        neuesLernziel_Topic.learningObjectivesTopic.add(neuesLernziel)
        return render(request, 'lernziel.html', {'topic': topic.objects.all(), 'todo': todoList.objects.all()})
以及我使用的模板

<html lang="{{ LANGUAGE_CODE|default:"de-de" }}" >
<head>
<h1 align = "center">Lernziele</h1>
</head>

<body>

<form action="{% url 'create_lernziel' %}" method="post">
{% csrf_token %}
<br>Hallo Benutzer: {{ user.username }}</br>
Lernziel: <textarea name="Lernziel" rows="3" cols="45" ></textarea>
<p>
 <select name="Thema" size="5">
  {% for topic_ in topic %}
   <option>{{ topic_.topic }}</option>
  {% endfor %}
 </select>
</p>
<input type="submit" value="Absenden" />
</form>

{% comment %}
Here should be the table which displays learning objectives and the related topics
{% endcomment %}

</body>
</html>

勒恩齐尔
{%csrf_令牌%}

哈罗·贝努策:{{user.username}
勒恩齐尔: {topic%}中的topic_uu的% {{主题{.topic} {%endfor%}

{%comment%} 下面的表格显示了学习目标和相关主题 {%endcomment%}
好的,我知道我的问题有点奇怪,因为我没有直接错误的代码,我张贴。但是我试了很多次来正确地展示我想要的东西,但我就是不知道如何正确地去做。我的目标是有两列/标题:[学习目标]和[主题]。对于每个学习目标,我都希望有一个新的行。在每一行中,可以显示更多相关主题。如果您需要更多信息或希望我更具体地说明我的问题,请将其发布在评论中:)

编辑我对该结构的第一个想法是:我迭代学习目标,为每个目标创建一行,然后在该行中列出学习目标和主题。显然它不起作用了。桌子可能必须是动态的,这样我的想法才能起作用,或者我只是有一个错误的想法

<table border="1">
<th>Lernziel</th>
<th>Thema</th>

{% for topic_ in topic %}

        {% for lObj in topic_.learningObjectivesTopic.all %}
                <tr><td>{{ lObj }}</td>
        {% endfor %}

        <td>{{ topic_.topic }}</td></tr>

{% endfor %}

</table>

勒恩齐尔
西玛
{topic%}中的topic_uu的%
{主题{.learningObjectivesTopic.all%}中的lObj为%
{{lObj}}
{%endfor%}
{{主题{.topic}
{%endfor%}

这里没有什么复杂的东西。您已经知道,通过执行
my\u objective.topic\u set.all()
,可以从一个目标到它的相关主题。因此,您只需在遍历模板中的每个主题时执行此操作(因为它是一个模板,所以请删除括号):


{目标%中目标的百分比}
{{objective.learningobjections}
{objective.topic_set.all%}中的主题为%
{{topic.topic}{%if-forloop.last%},{%endif%}
{%endfor%}
{%endfor%}
models.py

class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')

class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField(learningObjective, verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')     
如果您想将主题放在前面,请使用“或”,即“learningObjective”


在模板中,删除“()“,我相信您知道如何在模板中使用它。

我不知道为什么在从db获得neuesLernziel_主题实例后,您仍要立即保存它-这似乎是您以前版本的遗留内容,但毫无意义。哦,是的,您是对的,我会更改它。您对此有什么疑惑?使用
主题集。所有
?也许反向关系会对您有所帮助。其工作原理,请参见此处:
class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')

class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField(learningObjective, verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')     
class topic(models.Model):
        learningObjectivesTopic = models.ManyToManyField("learningObjective", verbose_name = "Lernziel")
        topic = models.TextField(verbose_name = 'Thema')                         

class learningObjective(models.Model):
        learningObjectives = models.TextField(verbose_name = 'Lernziel')



<p>
 <select name="Thema" size="5">
  {% for topic_ in topic %}
   <option>{{ topic_.topic }}</option>
   {% for lo in topic_.learningObjectivesTopic.all %}
        <option>{{ lo.learningObjectives }}</option>
   {% endfor %}
  {% endfor %}
 </select>
</p>
lo = learningObjective.objects.all()[0]
lo.topic_set()