如何将 Django 字段(如字段对象中的字段)保存到数据库?

发布于 2024-12-10 22:51:29 字数 506 浏览 0 评论 0原文

我正在尝试实现一个可重用的表单,用户可以下载 HTML 格式的源代码,该源代码是由网站为他们生成的。他们还可以使用界面创建表单,在界面中选择所需的字段并对它们进行排名,以便最高的字段首先出现在可下载的表单中。

然后,他们可以在其网站中使用该表单来收集客户数据,当提交表单时,操作参数会指向我们的网站,以便我们可以将信息存储在我们的服务器上。

现在,我的问题是,如何在数据库中保存 Django Field 对象?这样我就可以存储用户创建的表单,尤其是他们选择的字段。

我需要存储字段,以便我可以创建通用的 Django ModelForm,并且用户可以将该 ModelForm 的字段保存在可下载的表单中。因此,当他们将信息提交给我们时,我可以将信息直接提供给 ModelForm(因为字段是可识别的)并进行一些进一步的验证。

另外,我还必须为每个字段添加一些基本的 javascript 验证(如validation.js)(当用户下载表单时必须存在),而且我知道如果我可以自己保存字段会更容易。

I'm trying to implement a reusable form that users can download the source code in HTML which was site generated for them by the site. They can also create their forms using an interface where they get to choose the fields they want and rank them so the highest would appear first in the downloadable form.

They can then use the form in their sites to gather customer data and when the form gets submitted, the action parameter points back to our site so we can store the information on our servers.

Now, my question is, how can I save a Django Field object in my database? This is so I can store the forms that the users created, especially the Fields that they chose.

I need to store the Fields so that I can create a generic Django ModelForm and the users can save the Fields of that ModelForm in their downloadable forms. So when they submit back the information to us I can feed the information straight to the ModelForm (since the Fields would be recognizable) and do some further validation.

Also I have to add some basic javascript validations (like validation.js) for each field (must be present when the user downloads the form), and I know it will be easier if I can save the Fields themselves.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2024-12-17 22:51:30

您可以腌制 Field 对象:

import cPickle
field = forms.CharField()
dump = cPickle.dumps(field) # serializes field as an ASCII string
my_model.stored_field = dump

要取消腌制:

field = cPickle.loads(my_model.stored_field)

http://docs.python.org/library/pickle .html

You could pickle the Field object:

import cPickle
field = forms.CharField()
dump = cPickle.dumps(field) # serializes field as an ASCII string
my_model.stored_field = dump

To unpickle:

field = cPickle.loads(my_model.stored_field)

http://docs.python.org/library/pickle.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文