Python Django保存多个内联表单集
切换到内联表单集后,我得到了:Python Django保存多个内联表单集,python,django,Python,Django,切换到内联表单集后,我得到了: def dns_view(request, domain): dnszone = get_object_or_404(DNSSQL, zone = domain) form1 = EditDNSZone(instance = dnszone) forms = EditDNSEntry(instance = dnszone, prefix = 'entries') formsmx = EditDNSEntryMX(instance
def dns_view(request, domain):
dnszone = get_object_or_404(DNSSQL, zone = domain)
form1 = EditDNSZone(instance = dnszone)
forms = EditDNSEntry(instance = dnszone, prefix = 'entries')
formsmx = EditDNSEntryMX(instance = dnszone, prefix = 'mxentries')
尝试保存所有表单后,我只保存了form1。
如何保存所有表单?Django的表单集用于同一表单的多个实例。您正试图保存多个表单类,这不是formset的用途 一种方法是构建一个表单,该表单包含要包含的表单中的所有字段,并且在处理表单时,创建要处理的每个表单。下面是一个简单的例子。您也可以通过内省模型和自动创建模型表单来做一些花哨的事情,但这是一个很长的故事
class Form1(forms.Form):
a_field = forms.CharField()
class Form2(forms.Form):
b_field = forms.CharField()
class MainForm(forms.Form):
a_field = forms.CharField()
b_field = forms.CharField()
def __init__(self, **kwargs):
super(MainForm, self).__init__(**kwargs)
# This will work because the field name matches that of the small forms, data unknow to
# a form will just be ignored. If you have something more complex, you need to append
# prefix, and converting the field name here.
form1 = Form1(**kwargs)
form2 = Form2(**kwargs)
Django的表单集用于同一表单的多个实例。您正试图保存多个表单类,这不是formset的用途 一种方法是构建一个表单,该表单包含要包含的表单中的所有字段,并且在处理表单时,创建要处理的每个表单。下面是一个简单的例子。您也可以通过内省模型和自动创建模型表单来做一些花哨的事情,但这是一个很长的故事
class Form1(forms.Form):
a_field = forms.CharField()
class Form2(forms.Form):
b_field = forms.CharField()
class MainForm(forms.Form):
a_field = forms.CharField()
b_field = forms.CharField()
def __init__(self, **kwargs):
super(MainForm, self).__init__(**kwargs)
# This will work because the field name matches that of the small forms, data unknow to
# a form will just be ignored. If you have something more complex, you need to append
# prefix, and converting the field name here.
form1 = Form1(**kwargs)
form2 = Form2(**kwargs)
好的,关于什么是最好的方法有什么建议吗?好的,关于什么是最好的方法有什么建议吗?