3

我无法显示具有多个实例对象的内联表单集。我想要所有的列表Owner和所有它们的内联表单集Pet,都是一页。

下面的代码有效,但一次调用 1 个所有者对象。有什么建议么?

这是一个来自以下的新问题:Django DatabaseError "more than one row returned by a subquery used as an expression" Editable related fields to object

模型.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)
   owner = owners[0]

   if request.method == "POST":
      petNameInlineFormSet = PetNameFormSet(request.POST, request.FILES, instance=owner)

      if petNameInlineFormSet.is_valid():
         petNameInlineFormSet.save()

         return HttpResponseRedirect(reverse('success'))

   else:
      petNameInlineFormSet = PetNameFormSet(instance=owner) 

   context = {'teacher': teacher, 'owner': owner, 'petNameInlineFormSet' : petNameInlineFormSet}
   return render(request, 'petname.html', context)

HTML 宠物名.html

   {% load url from future %}
 <form class="petname_form" action="{% url "petname" teacher.id %}" method="post">{% csrf_token %}
   {{ teacher }}
   {{ owner.as_table }}
   {{ petNameInlineFormSet.as_table }}
 </form>

更新

HTML

{% for owner_form in owner_forms %}
    <form class="petname_form" action="{% url "petname" teacher.id %}" method="post">{% csrf_token %
     {% for o in owners %}
      {{o.owner}} has pets:<br/> //owner is the PK of the Owner model
         {{ owner_forms.relevantteacher }}
         {{ owner_forms.as_table }}
         {{ owner_forms.inline_form.as_table }}
     {% endfor %}
    </form>
4

1 回答 1

1

基本上,您需要做的是编译要呈现的所有者表单列表。验证后,您必须为每个表单保留错误和成功消息,并呈现结果。

这是一个基于您的views.py的半伪代码示例,用于实现上述目标:

owner_forms = []

if request.method == "POST":
    for owner in owners:
        #passing instance here may yeild unexpected behavior; django is aware of instance based on request.POST data.
        owner_form = PetNameFormSet(request.POST, request.FILES) 
        owner_forms.append(owner_form)

        if petNameInlineFormSet.is_valid():
            petNameInlineFormSet.save()

else:
    for owner in owners:
        owner_form = PetNameFormSet(instance=owner)
        owner_forms.append(owner_form)

context['owner_forms'] = owner_forms

现在,在 owner_forms 中渲染每个表单:

{% for owner_form in owner_forms %}
    <form class="petname_form" action="{% url "petname" teacher.id %}" method="post">{% csrf_token %}
       {% refer to the teacher object associated with the object the form is generated on %}
       {{ owner_form.relevantteacher }}
       {{ owner_form.as_table }}
       {{ owner_form.inline_form.as_table }}
    </form>
{% endfor %}

其他建议:

  1. max_length=300取决于所使用的数据库是有问题的。一般的经验法则是将 CharField 限制为 255 个字符。任何大于 255 的都应该是 TextField。
  2. Django 允许懒惰地引用 ForeignKeys,替换 with 的实例models.ForeignKey(Model)models.ForeignKey("Model")防止将来出现潜在的竞争条件。
于 2013-08-06T06:36:00.073 回答