Django 动态表单保存

发布于 2024-08-12 21:36:14 字数 444 浏览 4 评论 0 原文

我正在使用 James Bennetts 代码(链接文本 )创建动态表单。一切工作正常,但我现在已经到了需要保存数据的地步,并且变得有点卡住了。我知道我可以评估表单返回的数据,然后将其作为字符串保存到数据库中,但我真正想做的是保存它的数据类型,例如日期、整数、varchar 以及值,以便当涉及到查看数据时,我可以根据数据的类型对其进行一些处理,例如获取大于上周的日期。

所以我的问题是如何根据表单元素的类型来访问表单元素的数据库类型,例如 django.forms.IntegerField 的数据库字段类型为 int,django.forms.DateField 将是日期字段, django.forms.ChoiceField 将是一个 varchar 字段?

I am using James Bennetts code (link text) to create a dynamic form. Everything is working ok but I have now come to the point where I need to save the data and have become a bit stuck. I know I can assess the data returned by the form and simply save this to the database as a string but what I'd really like to do is save what type of data it is e.g. date, integer, varchar along with the value so that when it comes to viewing the data I can do some processing on it depending on what type it is e.g. get dates greater than last week.

So my question is how do I access what database type the form element is based on what type of form element it is e.g. a django.forms.IntegerField has a database field type of int, django.forms.DateField would be a date field and django.forms.ChoiceField would be a varchar field?

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

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

发布评论

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

评论(3

浅笑轻吟梦一曲 2024-08-19 21:36:14

为什么你想知道你正在使用什么样的数据库字段?您是否通过原始 SQL 存储表单中的信息?您应该有一些模型,您可以存储表单中的信息,它将为您完成所有工作。

也许您可以显示一些表单代码?现在很难确定你到底想做什么。

Why do you want to know, what kind of database field you are using? Are you storing information from the form through raw sql? You should have some model, that you are storing information from the form and it will do all the work for you.

Maybe you could show some form code? Right now it's hard to determine, what exactly you are trying to do.

空气里的味道 2024-08-19 21:36:14

我无法理解确切的问题,所以如果我弄错了,请原谅我。

如果您正在使用模型,那么您不需要了解数据库级数据类型。它们是由 django 根据您的模型字段定义的。

但是,由于您正在谈论动态表单(我已阅读过这篇文章),因此您可能没有使用模型,至少没有直接使用模型。在这种情况下,这也不重要,因为您正在使用表单验证,因此,例如,您可以绝对确定整数来自 forms.IntegerField 字段,unicode 来自forms.CharField 等等。

如果您手动编写数据库交互例程(原始 sql),那么您必须自己将 python 类型映射到 db 类型,例如 转到 a integer 类型(或其他)的列, 转到 datetime 类型的列(或不,这个例子是任意的)等等。当您使用模型时,django 以独立于数据库引擎的方式为您完成这种类型的映射。

无论哪种方式,您自己都在 python 端定义数据类型,并且您或 django 也必须在 db 端定义数据类型。有时,这些类型的选择不是自动的 1:1 类型决策,而是基于此数据在应用程序中的用途的设计决策。

抱歉,如果这没有什么意义,但是,我必须承认,我不太明白你问题背后的问题。

I can not understand the exact problem, so forgive me if I get things wrong.

If you are using models, then you don't need to know about database-level data types. They are defined by django according to your model fields.

However, since you are talking about dynamic forms (I've read the article), you are probably not working with models, at least not directly. In that case, it should not matter as well, because you are using form validation so, for example, you can be absolutely sure that an integer comes out of a forms.IntegerField field, unicode comes out of forms.CharField and so on.

If you are writing your database-interaction routies by hand (raw sql), then you have to map python-types to db-types yourself, for example <type 'int'> goes to a column of type integer (or something), <type 'datetime.datetime'> goes to a datetime type of column (or not, this example is arbitrary) and so on. When you are using models, django does this type of mapping for you in a database-engine-independent way.

Either way, you, yourself are defining the datatypes on the python side and you or django must also define the datatypes on the db side. The choice of those types is, at times, not an automatic 1:1 type of decision, but, rather, a design decision, based on what this data is used for in your application.

Sorry, if this makes little sense, but, I must admit, that I don't quite understand the problem behind your question.

若有似无的小暗淡 2024-08-19 21:36:14

如果您使用 James 的代码,那么您不会从表单本身中获得Model,而是获得表单字段元素的列表。这意味着您无法将数据保存为Model 实例。

我认为你有两个选择;将整个表单捆绑到 JSON 对象中,并将其保存到数据库中的 LONGTEXT 变量中,或者将每个表单元素单独保存到数据库的一行中,然后保存到 BLOB 条目中。在这种情况下,您需要在保存对象之前对其进行“pickle”。如果您 p​​ickle 对象并将其保存到数据库中,那么当您检索它并取消pickle 时,您将拥有与该对象关联的所有 python 类信息。

试图让这一点更清楚 - 如果你有字节; 2009-11-28 21:34:36.516176,这是一个 str 还是 datetime 对象?您无法判断它是作为 VARCHAR 还是 LONGTEXT 存储在数据库中。这是您问题的核心 - 如果您将其保存为腌制对象,您确实会获得对象信息。

通过扩展,您可以将整个 Form 对象作为 JSON 对象保存到数据库中,或者腌制该对象并保存它。

我目前正在努力解决非常类似的问题,因为我正在尝试组合一个动态表单系统,并考虑使用“单独的表单字段元素,腌制”,然后保存到数据库中。那么我就来看看这个问题是如何解决的! :)

If you're using James' code, then you don't get a Model out of the form per se, rather a list of form field elements. That means that you can't save the data as a Model instance.

I think you have two choices; bundle the whole form into a JSON object and save that into a LONGTEXT variable in your database, or save each form element into a row of the database on its own, saving it into a BLOB entry. In this case, you'll need to 'pickle' the object before saving it. If you pickle the object and save it into the database, when you retrieve it and unpickle it, you'll have all the python class information associated with the object.

Trying to make this clearer - if you have the bytes; 2009-11-28 21:34:36.516176, is this a str or a datetime object? You can't tell if it's stored in the database as a VARCHAR or LONGTEXT. Which is the core of your question - you do get object information if you save it as a pickled object though.

By extension, you could save your whole Form object into the database, either as a JSON object, or pickle the object and save that.

I'm struggling with something very similar at the moment, as I'm trying to put together a dynamic form system, and thinking of going the 'individual form field element, pickled' and then saved into the database. So I'll be watching how this question works out! :)

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