Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango DatabaseError "more than one row returned by a subquery used as an expression" Editable related fields to object
    primarykey
    data
    text
    <p>I am trying to add inlines to my template but continue to get a Database error:</p> <pre><code> more than one row returned by a subquery used as an expression </code></pre> <p>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?</p> <p><strong>models.py</strong></p> <pre><code>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) </code></pre> <p><strong>forms.py</strong></p> <pre><code>class OwnerForm(forms.ModelForm): class Meta: model = Owner PetNameFormSet = inlineformset_factory(Owner, PetName, can_delete=False, extra=3, form=OwnerForm) </code></pre> <p><strong>views.py</strong></p> <pre><code>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) </code></pre> <p><strong>Update:</strong></p> <p>Here is the traceback:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload