当我试图将一个具有M到M关系的属性与另一个属性相关联时,收到了以下错误信息:
get()返回了不止一个主题——它返回了2个!
你们能告诉我这是什么意思,也许提前告诉我如何避免这个错误吗?
models
class LearningObjective(models.Model):
learning_objective=models.TextField()
class Topic(models.Model):
learning_objective_topic=models.ManyToManyField(LearningObjective)
topic=models.TextField()
< p > LearningObjective.objects.all()
的输出 [<LearningObjective: lO1>, <LearningObjective: lO2>, <LearningObjective: lO3>]
< p > Topic.objects.all()
的输出结果 [<Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>, <Topic: Topic object>]
观点
def create_themen(request):
new_topic=Topic(topic=request.POST['topic'])
new_topic.save()
return render(request, 'topic.html', {'topic': topic.objects.all()})
def create_learning_objective(request):
new_learning_objective=LearningObjective(learning_objective=request.POST['learning_objective'])
new_learning_objective.save()
new_learning_objective_topic=Topic.objects.get(topic=request.POST['topic'])
new_learning_objective_topic.new_learning_objective_topic.add(new_learning_objective)
return render( request, 'learning_objective.html', {
'topic': Topic.objects.all(),
'todo': TodoList.objects.all(),
'learning_objective': LearningObjective.objects.all()
})