我正在尝试将内联添加到我的模板中,但继续收到数据库错误:
more than one row returned by a subquery used as an expression
我的 models.py 中有 3 个相互关联的对象。用户将能够看到选择了哪个教师并列出了该教师下的所有所有者(教师和所有者将仅显示为不可编辑的列表)。我想列出所有者下的所有宠物并进行编辑。关于我为什么收到此错误的任何想法?以及我如何才能实现我的目标?
模型.py
class Teacher(models.Model):
teacher = models.CharField(max_length=300)
class Owner(models.Model):
relevantteacher = models.ForeignKey(Teacher)
owner = models.CharField(max_length=300)
class PetName(models.Model):
relevantowner = models.ForeignKey(Owner)
pet_name = models.CharField(max_length=50)
表格.py
class OwnerForm(forms.ModelForm):
class Meta:
model = Owner
PetNameFormSet = inlineformset_factory(Owner,
PetName,
can_delete=False,
extra=3,
form=OwnerForm)
视图.py
def petname(request, teacher_id):
teacher = get_object_or_404(Teacher, pk=teacher_id)
owners = Owner.objects.filter(relevantteacher=teacher_id)
if request.method == "POST":
petNameInlineFormSet = PetNameFormSet(request.POST, request.FILES, instance=owners)
if petNameInlineFormSet.is_valid():
petNameInlineFormSet.save()
return HttpResponseRedirect(reverse('success'))
else:
petNameInlineFormSet = PetNameFormSet(instance=owners) //error might be here?
context = {'teacher': teacher, 'owners': owners, 'petNameInlineFormSet' : petNameInlineFormSet}
return render(request, 'petname.html', context)
更新:
这是回溯:
File "hde/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/views.py" in petname
60. petNameInlineFormSet = PetNameFormSet(instance=owners)
File "lib/python2.7/site-packages/django/forms/models.py" in __init__
697. queryset=qs, **kwargs)
File "lib/python2.7/site-packages/django/forms/models.py" in __init__
424. super(BaseModelFormSet, self).__init__(**defaults)