为Django中的540个字段创建输入表单。最好的方法?

2024-04-26 07:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是刚到django的,想听听你对如何创建一个表单的看法 有540个字段的表。最好的方法是什么?最好将modelform拆分为多个组件,还是创建一个模板来收集部分输入(每页多个输入)并遍历所有字段?如果你能给我提供一些信息和/或例子就太好了。 谢谢。你知道吗


Tags: django方法模板信息表单组件例子每页
2条回答

您可以考虑的一种方法是将您的模型拆分为语义片段,每个片段都是一个单独的模型,具有更多可消化的字段。你知道吗

然后使用一对一关系(由OneToOneField实现)将这些“切片模型”映射回主对象。你知道吗

在您的向导中,您可以在事务开始时启动事务,并且只有在一切正常运行时才提交。你知道吗

Django有一个Form wizard可以存储跨多个页面累积的信息,验证每一步。你知道吗

How it works

Here’s the basic workflow for how a user would use a wizard:

  1. The user visits the first page of the wizard, fills in the form and submits it.
  2. The server validates the data. If it’s invalid, the form is displayed again, with error messages. If it’s valid, the server saves the current state of the wizard in the backend and redirects to the next step.
  3. Step 1 and 2 repeat, for every subsequent form in the wizard.
  4. Once the user has submitted all the forms and all the data has been validated, the wizard processes the data – saving it to the database, sending an email, or whatever the application needs to do.

相关问题 更多 >