简体   繁体   English

Django表单和子表单的方法

[英]Django methodology for forms and subforms

This is an open question. 这是一个悬而未决的问题。 I am trying to write HTML pages for defining / editing questions (database objects). 我正在尝试编写HTML页面以定义/编辑问题 (数据库对象)。 A question is composed of a small text and a type: the type indicates its declination . 一个问题由一个小文本和一个类型组成:该类型表示其偏斜 The question declinations are, for example, slider , yes-no , free-text or multiple choice . 问题倾向例如 滑块是-否自由文本多项选择 For the free-text and yes-no declinations, no further information is needed. 对于自由文本和“ 是-否” ,不需要进一步的信息。 But for the slider and multiple choice ones, additionnal information have to be entered. 但是对于滑块多项选择 ,必须输入其他信息。

Now what I done / tried is create form, view and template to define a generic question, and create sub forms, views and templates for each question declination that needs additionnal fields. 现在,我完成/尝试过的工作是创建表单,视图和模板以定义通用问题,并为每个需要附加字段的问题磁偏角创建 表单,视图和模板

The problem is that I encounter difficulties to keep the generic question id in the sub forms to attach the additionnal fields to the question. 问题是我在将通用问题ID保留在子表单中以将附加字段附加到问题时遇到困难。 I tried to pass it to the sub view, but I must have access to it in the form too to save the database object with the question reference, so it means adding a hidden input field in the form, which leads to other issues, etc etc... 我试图将其传递给子视图,但是我也必须以表单的形式访问它,以使用问题引用保存数据库对象,因此这意味着在表单中添加一个隐藏的输入字段,这会导致其他问题,等等。等等...

What I lack is web-development experience... The better thing I could get is dynamic drop-down menus adapting to the selected question type , but I do not know javascript or ajax language for now, so a few different views/templates would be enough. 我缺乏的是Web开发经验...我能得到的更好的是适应所选问题类型的动态下拉菜单,但目前我还不知道Javascript或Ajax语言,因此会有一些不同的视图/模板足够。

What would be the easiest / best way to achieve this ? 实现这一目标的最简单/最佳方法是什么? I am not asking for code but rather something like a methodology. 我不是在要求代码,而是在要求某种方法。

Please let me know if I missed something that makes the question incomprehensible. 如果我错过了使问题难以理解的内容,请告诉我。

Your methodology seems fine to me (creating base view, form class and template that would be inherited and customized for each declination), except for the model part. 对我来说,您的方法似乎不错(创建基本视图,表单类和模板,这些模板将为每个磁偏角继承和自定义),但模型部分除外。

Since your declinations may have some custom fields, I don't think it is efficient to use the same model for all your questions type. 由于您的偏倚可能包含一些自定义字段,因此我认为对所有问题类型使用相同的模型效率不高。

I would use model inheritance , as follows: 我将使用模型继承 ,如下所示:

class BaseQuestion(models.Model):
    """Your base model for all questions"""
    title = models.CharField(max_length=255)
    description = models.TextField()

class YesNoQuestion(BaseQuestion):
    pass

class FreeTextQuestion(BaseQuestion):
    pass

class MultipleChoicesQuestion(BaseQuestion):
    # assuming you have a QuestionChoice model to store your available choices
    choices = models.ManyToManyField(QuestionChoice)

class SliderQuestion(BaseQuestion):
    slider_start = models.IntegerField(default=1)
    slider_end = models.IntegerField(default=10)

Please refer to Django documentation for an in-depth explanation about model-inheritance, but there is one thing you need to know: it will impact performance, especially on large databases. 请参考Django文档以获取有关模型继承的深入说明,但是您需要了解一件事:它将影响性能,尤其是在大型数据库上。 However, in your situation, it seems the way to go. 但是,根据您的情况,这似乎是可行的方法。

Also note that some django applications such as django-polymorphic will probably help you working with model inheritance. 还要注意,某些django应用程序(例如django-polymorphic)可能会帮助您处理模型继承。

For the rest of the process, well, I would follow this process: 在剩下的过程中,我将遵循以下过程:

  • A view that handle the base form for a question (title and description fields), plus an additionnal field with the list of all available question declinations 处理问题基本形式的视图(标题和描述字段),以及带有所有可用问题偏倚列表的附加字段
  • when the user select a type, you fire an ajax request that will post the form data. 当用户选择一种类型时,您将触发一个ajax请求,该请求将发布表单数据。 The view does not save anything but, instead, return another form, corresponding to the selected declination, and filled with previously entered values. 该视图不保存任何内容,而是返回另一种形式,该形式对应于选定的磁偏角,并用先前输入的值填充。 This way, you don't care about keeping ID, since a whole new form is returned. 这样,您将不必担心保留ID,因为会返回一个全新的表单。
  • on post, this form should trigger another, dedicated view for the declination, that will process the data and save an instance of the corresponding Question model into database 发布后,此表单应触发磁偏角的另一个专用视图,该视图将处理数据并将相应Question模型的实例保存到数据库中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM