gpt4 book ai didi

django - 同时调用内联 FormSet 的多个实例

转载 作者:行者123 更新时间:2023-12-02 06:41:31 25 4
gpt4 key购买 nike

我在显示具有多个实例对象的内联表单集时遇到问题。我想要一份所有 Owner 的列表以及他们所有 Pet 的内联表单集,全部一页。

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

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

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

最佳答案

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

这是一个基于您的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 允许惰性引用外键,用 models.ForeignKey("Model") 替换 models.ForeignKey(Model) 的实例,以防止潜在的竞争条件 future 。

关于django - 同时调用内联 FormSet 的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18069568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com