寻求良好的方法来保存通过动态 Django 表单提交的数据

发布于 2024-11-02 16:12:05 字数 1867 浏览 1 评论 0原文

摘要:

寻找一种将数据保存到动态生成关联表单的 Django 模型的好方法。

详细信息

我一直在思考创建由模型支持的动态 Django 表单的最佳方法。例如,我想创建一个界面,用户可以在其中创建 HTML 表单,动态自定义该表单中的字段类型(数字、字符串、下拉框、日期等),然后将该表单显示给其他人用户,以便这些用户可以提交保存到数据库的数据。我不确定如何采取有效的方法来保存数据。

www.formsite.comwww.mailchimp.com 有一些表单构建工具,它们是我正在尝试做的事情的很好的例子。 Jacob Kaplan-Moss 有一个关于如何动态创建表单的优秀教程,但是教程没有涉及如何保存数据。

作为一个虚拟示例,一种(也许不好?)方法可能是创建一些如下所示的模型,其中有一个用于 SurveyQuestions 的数据库表(存储每个字段的可自定义名称和数据类型),还有一个用于 SurveyQuestionResponses 的数据库表(每个记录存储对特定调查中的调查问题的个人回答)。

然而,这种方法似乎可能会导致非常复杂且缓慢的查询。例如,如果一项调查有 10 个问题,并且您希望显示该调查的 10 个用户响应,则将有一个查询来选择所有 10 个调查问题,然后对于每个调查响应者,将有一个查询来选择每个 SurveyQuestionResponse。看起来所需的查询数量可能会很快增加!

class Survey(models.Model):
    # some fields here. 
    pass

class SurveyQuestion(models.Model):
    """ Defines the headings and field
        types for a given Survey. 
    """
    survey = models.ForeignKey(Survey)
    field_name = models.CharField(
        max_length=255, 
        help_text='Enter the name for this field heading')
    field_type = models.IntegerField(
        choices=choices.FIELD_TYPES,  
        help_text='Enter the data type for this field')
    display_order = models.IntegerField(default=0)

class SurveyQuestionResponse(models.Model):
    survey_field = models.ForeignKey(SurveyQuestion)
    response value = models.TextArea(blank=True, null=True)

有没有更好的方法来持久化基于动态表单的数据?我是否应该以某种方式将表单受访者的响应转换为某种腌制格式并将其存储到 TextField(而不是有 10 个 SurveyQuestionResponse 记录,而是一条记录将所有响应值腌制在一起)?我对 NoSQL 选项不太熟悉,但是 NoSQL 方法最适合这种类型的事情吗?是否有某种有意义的渲染或缓存?

我经常遇到这样的情况:从动态表单中保存数据非常有用。我想知道其他人的方法是什么。非常感谢任何建议。感谢您阅读这个公认的长问题。

Summary:

Looking for a good way to save data to Django models for which the associated forms are generated dynamically.

Detail:

I've been puzzling over the best approach for creating dynamic Django forms backed by models. For example, I'd like to create an interface where a user can create an HTML form, customize the types of fields in that form dynamically (Number, String, Dropdown Box, Date, etc.), and then display that form to other users so those users can submit data which is saved to a database. I'm not sure how to make an efficient approach to persist the data.

www.formsite.com and www.mailchimp.com have some form-building tools that are nice examples of what I am trying to do. Jacob Kaplan-Moss has an excellent tutorial on how to create the forms dynamically, but the tutorial doesn't get into how to persist the data.

As a dummy example, one (perhaps bad?) approach might be to create some models like below, where there is a database table for the SurveyQuestions (storing the customizable names and datatypes of each field) and one for the SurveyQuestionResponses (each record storing an individual response for a SurveyQuestion on a particular Survey).

However, it seems like this approach might result in really complex and slow queries. For example, if a Survey has 10 questions and you would like to display 10 user responses to that survey, there would be queries to select all 10 SurveyQuestions and then for each survey responder, there would be a query to select each of the SurveyQuestionResponses. It seems like the number of queries needed could add up really fast!

class Survey(models.Model):
    # some fields here. 
    pass

class SurveyQuestion(models.Model):
    """ Defines the headings and field
        types for a given Survey. 
    """
    survey = models.ForeignKey(Survey)
    field_name = models.CharField(
        max_length=255, 
        help_text='Enter the name for this field heading')
    field_type = models.IntegerField(
        choices=choices.FIELD_TYPES,  
        help_text='Enter the data type for this field')
    display_order = models.IntegerField(default=0)

class SurveyQuestionResponse(models.Model):
    survey_field = models.ForeignKey(SurveyQuestion)
    response value = models.TextArea(blank=True, null=True)

Is there a better approach to persisting data based on dynamic forms? Should I be somehow converting a form respondent's response to some sort of pickled format and store it to a TextField (Instead of having 10 SurveyQuestionResponse records there would be one record with all the response values pickled together)? I'm not too familiar with NoSQL options, but would a NoSQL approach work best for this type of thing? Is there some sort of rendering or caching that would make sense to do?

I keep encountering situations where saving data from dynamic forms like this would be very useful. I am wondering what other people's approaches are. Any advice is much appreciated. Thanks for reading this admittedly long question.

Joe

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

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

发布评论

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

评论(1

一身骄傲 2024-11-09 16:12:05

对于关系数据库,可以使用实体属性值模型(EAV)来实现动态或开放模式。关系数据库并不真正适合这种类型的模式,并且随着时间的推移,这通常会导致查询非常慢。 NoSQL 有它自己的一系列问题,但我认为它最适合您的要求。如果您决定走这条路,您可以看看 MongoDB。我没有广泛使用它,但它似乎比其他 NoSQL 数据库更类似于关系数据库,而且它的 python 界面看起来与 django 的 ORM 非常相似。到那时,我记得找到了一个很好的 Django EAV 示例。虽然我现在不记得在哪里。

For a relational database an Entity-attribute-value model(EAV) could be used to achieve a dynamic, or open schema. Relational databases are not really suited for this type of schema, and this generally results in very slow queries over time. NoSQL has its own set of issues but I think that it would be best suited to your requirements. If you decide to take this route you can take a look at MongoDB. I have not used it extensively, but it seems most similar to relational database than the other NoSQL database out there, and its python interface seems pretty similar to django's ORM. By the was I remember finding a nice EAV example for Django. Though I don't remember where at the moment.

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