gpt4 book ai didi

python - Django - 使用 ManyToMany 保存表单集

转载 作者:太空宇宙 更新时间:2023-11-03 19:11:38 27 4
gpt4 key购买 nike

我遇到了以下问题。当我尝试保存已编辑的对象(详细信息如下)时,我收到错误:

MultiValueDictKeyError at /apps/edit/1/

"Key 'application2server_set-0-id' not found in <QueryDict: {u'application2server_set-MAX_NUM_FORMS': [u''], u'name': [u'application1'], u'repository': [u'1'], u'application2server_set-INITIAL_FORMS': [u'2'], u'application2server_set-TOTAL_FORMS': [u'5'], u'csrfmiddlewaretoken': [u'bmEPdLCloNHKR0qUmwdhdmQ4aDKHV2CT'], u'servers': [u'1', u'2']}>"

以下是使用的模型和表格:

class Server(models.Model):
host = models.CharField(max_length=100)
user = models.CharField(max_length=20)
root_directory = models.CharField(max_length=200)


class Repository(models.Model):
host = models.CharField(max_length=100)
user = models.CharField(max_length=20)


class Application(models.Model):
name = models.CharField(max_length=100)
repository = models.ForeignKey(Repository)
servers = models.ManyToManyField(Server, through='Application2Server', null=True, blank=True)


class Application2Server(models.Model):
application = models.ForeignKey(Application)
server = models.ForeignKey(Server)
tag = models.CharField(max_length=100)
deployment_date = models.DateTimeField(auto_now=True, blank=True)


class ServerForm(ModelForm):
class Meta:
model = Server


class RepositoryForm(ModelForm):
class Meta:
model = Repository


class ApplicationForm(ModelForm):
class Meta:
model = Application


class Application2ServerForm(ModelForm):
class Meta:
model = Application2Server
exclude = ('application', 'server', 'tag')

查看用于保存日期:

def app_edit(request, id):
application = get_object_or_404(Application, pk=id)
Application2ServerFormSet = inlineformset_factory(Application, Application2Server)

if request.method == 'POST':
form = ApplicationForm(request.POST, instance=application)
formset = Application2ServerFormSet(request.POST, instance=application)

if form.is_valid() and formset.is_valid():
saved_application = form.save()
formset.save()

return HttpResponseRedirect(reverse(
'proj.views.app_show',
args=(saved_application.pk,)
))
else:
form = ApplicationForm(instance=application)
formset = Application2ServerFormSet(instance=application)

return render(request, 'app_edit.html', {'form': form, 'formset': formset})

模板生成 HTML 和表单本身:

<h1>App edit view</h1>
{% extends "base.html" %}

{% block container %}
<form method="post">{% csrf_token %}
<table>
{{ formset.management_form }}

{{ form.as_table }}
<tr><th></th><td><input type="submit" value="Save changes" /></td></tr>
</table>
</form>
{% endblock %}

任何帮助将不胜感激!谢谢!

最佳答案

您需要在表单集中以及模板中呈现每个表单。

将您的模板更新为类似这样的内容(您在其中呈现表单集):

<form method="post" action="">
{{ formset.management_form }}
<table>
{% for form in formset %}
{{ form.as_table }}
{% endfor %}
</table>
</form>

引用:Formset in templates

关于python - Django - 使用 ManyToMany 保存表单集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769125/

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