【问题标题】:Many to many relationships in django; refering from one object to anotherdjango 中的多对多关系;从一个对象指向另一个对象
【发布时间】: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


    【解决方案1】:

    问题不在于那一行:它在于前两行,它们明确地创建了一个新主题。如果您想将您的目标与现有主题相关联,您需要使用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 发布/查询,而不是“主题”的文本。

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 2020-12-21
      • 2010-10-20
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 2013-11-16
      • 2015-02-18
      • 1970-01-01
      相关资源
      最近更新 更多