Django-创建用于编辑模型的多个实例的表单 [英] Django - Creating form for editing multiple instance of model
问题描述
注意:Django / Python初学者,希望这个问题清楚
Note: Django/Python beginner, hope this question is clear
我需要创建一个表格,其中可以一次编辑多个模型实例
I need to create a form where multiple instances of a model can be edited at once in a single form, and be submitted at the same time.
例如,我有两个模型,邀请和访客,其中多个访客可以与一个邀请相关联。我需要一个表格,即可编辑附加到邀请中的所有来宾的详细信息,同时提交并保存到数据库。
For instance, I have two models, Invite and Guest, where multiple Guests can be associated with a single Invite. I need a single form where I'm able to edit particular details of all Guests attached to the invite, submit at the same time, and save to the database.
I已经看到有关使用脆皮形式的一些建议,但还没有
I've seen a few suggestions about using crispy-forms, but haven't managed to get it working.
我创建了一个提供某些输入的表单:
I've created a form that provides certain inputs:
from django import forms
from app.models import Guest
class ExtraForm(forms.ModelForm):
diet = forms.CharField(max_length=128, required=False)
transport = forms.BooleanField(initial=False)
# An inline class to provide additional information on the form.
class Meta:
# Provide an association between the ModelForm and a model
model = Guest
fields = ('diet', 'transport')
我的视图包括:
def extra_view(request, code):
invite = get_invite(code)
# Get the context from the request.
context = RequestContext(request)
# Get just guests labelled as attending
guests_attending = invite.guest_set.filter(attending=True)
if request.method == 'POST':
form = ExtraForm(request.POST)
print(form.data)
# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
# form.save(commit=True)
print(form)
return render(request, 'weddingapp/confirm.html', {
'invite': invite,
})
else:
# The supplied form contained errors - just print them to the terminal for now
print form.errors
else:
# # If the request was not a POST, display the form to enter details.
GuestForm = ExtraForm()
return render_to_response('weddingapp/extra.html',
{'GuestForm': GuestForm, 'invite': invite, 'guests_attending': guests_attending}, context)
最后,我的表单是:
<form id="extra_form" method="post" action="{% url 'weddingapp:extra' invite.code %}">
{% csrf_token %}
{% for guest in guests_attending %}
<fieldset class="form-group">
<h3>Form for {{ guest.guest_name }}</h3>
{% for field in GuestForm.visible_fields %}
{{ field.errors }}
<div>
{{ field.help_text }}
{{ field }}
</div>
{% endfor %}
</fieldset>
{% endfor %}
{{ form.management_form }}
<table>
{% for form in form %}
{{ form }}
{% endfor %}
</table>
<input type="submit" name="submit" value="Submit"/>
</form>
任何建议
推荐答案
您需要使用 FormSet
,尤其是 ModelFormSet :
...
GuestFormSet = modelformset_factory(Guest, form=ExtraForm)
使用它作为普通形式:
formset = GuestFormSet(data=request.POST)
if formset.is_valid():
formset.save()
在您的模板中:
<form method="post" action="">
{{ formset.management_form }}
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
</form>
提示:您可以避免避免这种样板
tip: you can avoid the avoid this boilerplate
if request.method == 'POST':
form = ExtraForm(request.POST)
print(form.data)
# Have we been provided with a valid form?
if form.is_valid():
带有简单快捷键:
form = ExtraForm(data=request.POST or None)
if form.is_valid():
...
这篇关于Django-创建用于编辑模型的多个实例的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!