nlr25
nlr25

Reputation: 1635

Django DatabaseError "more than one row returned by a subquery used as an expression" Editable related fields to object

I am trying to add inlines to my template but continue to get a Database error:

 more than one row returned by a subquery used as an expression

I have 3 objects in my models.py that relate to each other. The user will be able to see which Teacher is selected and have all Owners under that Teacher listed (Teacher and Owner will only appear as an uneditable list). I'd like to have all the Pets under the Owner listed and editable. Any ideas on why I am receiving this error? And how I may be able to accomplish my goal?

models.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)

forms.py

class OwnerForm(forms.ModelForm):
    class Meta:
       model = Owner

PetNameFormSet = inlineformset_factory(Owner,
    PetName,
    can_delete=False,
    extra=3,
    form=OwnerForm)

views.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)

Update:

Here is the traceback:

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)

Upvotes: 0

Views: 6320

Answers (2)

nlr25
nlr25

Reputation: 1635

Needed to pass only 1 object to the instance

owner = owners[0]

then

instance=owner

However, I am only able to add/edit pet names 1 owner at a time. Thanks aamir for the help!

Upvotes: 1

mattpic
mattpic

Reputation: 1285

I believe your error is in the second line of the views.py file. I believe it is the call to the get_object_or_404 method causing the error when you try to specify teacher.id in your template. The call to the get_object_or_404 method is returning more than one row from the database, so calling teacher.id is not possible on it from more than one row.

Upvotes: 0

Related Questions