Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django保存多个内联表单集_Python_Django - Fatal编程技术网

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)

好的,关于什么是最好的方法有什么建议吗?好的,关于什么是最好的方法有什么建议吗?