【发布时间】:2014-03-27 21:50:03
【问题描述】:
好的,伙计们,我有两个不同的模型:
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 个学习目标。例如主题 1 的 lObj 1 和 3,主题 2 的 lObj 2 和 4。发生的情况是,此时 django 在主题表中创建了 4 个新条目。它现在包含 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>
【问题讨论】:
标签: django django-models django-templates django-admin django-views