django中的多对多关系;从一个对象引用到另一个对象

时间:2014-02-25 15:06:47

标签: django django-models django-templates django-admin django-views

好的,我有两种不同的型号:

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')

非常基本。我的目标是创建属性“主题”的托管,然后在我想创建新的学习目标时引用它们。我的观点显然不符合我希望他们的工作方式,但我会发布这些观点,以便稍后解释我的目标是什么。

from django.shortcuts import render
from programm.models import *
from django.contrib.auth.decorators import login_required

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

@login_required(login_url='login')
def create_lernziel(request):
        neuesLernziel=learningObjective(learningObjectives=request.POST["Lernziel"])
        neuesLernziel.save()
        neuesLernziel_Topic=topic(topic=request.POST["Thema"])
        neuesLernziel_Topic.save()
        neuesLernziel_Topic.learningObjectivesTopic.add(neuesLernziel)
        return render(request, 'lernziel.html', {'topic': topic.objects.all()})

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

@login_required(login_url='login')
def create_themen(request):
        neueThemen=topic(topic=request.POST['Thema'])
        neueThemen.save()
        return render(request, 'themen.html', {'thema': topic.objects.all()})

我想到了这句话:

neuesLernziel_Topic.learningObjectivesTopic.add(neuesLernziel)

我可以参考我即将创建的学习目标和现有主题。但是看起来如何,我的django视图只是创建一个带有新id的新条目,而不是使用具有相同值和旧id的旧条目。为了更清楚:我有:topic1和topic2。我想现在提到4个学习目标。例如lOjj 1和3到主题1和lObj 2和4到主题2.会发生什么,目前django在主题表中创建了4个新的entrys。它现在包含3个值为topic1的赋值,3个赋值为topic2。它应该仍然是2个,并且在manytomany表中它应该使用odl id。如果您需要更多信息或有些不清楚的地方请写下来:)

顺便说一下,我收到了这两个模板的数据: 的 lernziel.html:

<!DOCTYPE html>

<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="3">
  {% for topic_ in topic %}
   <option>{{ topic_.topic }}</option>
  {% endfor %}
 </select>
</p>
<input type="submit" value="Absenden" />
</form>

<table border="1">

<th>Lernziel</th>
<th>Thema</th>

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

</table>

</body>
</html>

themen.html

<!DOCTYPE html>

<html>

<head>

</head>

<body>

<br>Hallo Benutzer: {{ user.username }}</br>

<form action="{% url 'create_themen' %}" method="post">
{% csrf_token %}
<br>Thema: <textarea name="Thema" rows="3" cols="45"></textarea></br>
<input type="submit" value="Absenden" />
</form>

</br>
<table border="1">
<th>Thema</th>
{% for thema_ in thema %}
<tr>
<td>{{ thema_.topic }}</td>
</tr>
{% endfor %}
</table>


</body>

</html>

1 个答案:

答案 0 :(得分:0)

问题不在于该行:它与前两行明确地创建了一个新主题。如果要将目标与现有主题相关联,则需要使用get从数据库中获取它,而不是实例化新目标并调用save()

neuesLernziel=learningObjective(learningObjectives=request.POST["Lernziel"])
neuesLernziel.save()
neuesLernziel_Topic = Topic.objects.get(topic=request.POST['Thema'])
neuesLernziel_Topic.learningObjectivesTopic.add(neuesLernziel)

您可能希望通过ID发布/查询,而不是“主题”文本。

相关问题