Dynamic shapes from variable length elements: wtforms

I use wtforms and I need to create something that will generate a form definition based on the information in the database; creating a dynamic form. I get an idea of ​​what needs to be done, and I just got started. I can create forms and use them with wtforms / flask, but defining forms from data that will be slightly different from form to form currently exceeds my current skill level.

Has anyone done this and offered any input? Most likely a vague question, there is no valid code yet. I did not find examples, but this is impossible to do.

mass of variable data to be used in a form --> wtforms ---> form on webpage 

EDIT:

So, for example, we can use polls. The overview consists of several SQLAlcehmy models. A survey is a model with any number of models of related questions (questions relate to surveys, and this is complicated, for example, by multiple choice questions). To simplify, use a simple json / dict pseudo-code for:

 {survey:"Number One", questions:{ question:{type:truefalse, field:"Is this true or false"}, question:{type:truefalse, field:"Is this true or false"}, question:{type:text, field:"Place your X here"} } } {survey:"Number Two", questions:{ question:{type:text, field:"Answer the question"}, question:{type:truefalse, field:"Is this true or false"}, question:{type:text, field:"Place your email address here"} } } 

Imagine instead several hundred different lengths with 5+ field types. How to use WTForms to manage forms for this, or do I even need to use wtforms? I can define static forms as needed, but not dynamically.

As an aside, I did something similar in rails with simpleform, but since I work in Python atm (on something else, I use the polling example as an example, but the question / field / answer theses on many types of input data that I needed).

So, yes, maybe I need to create some kind of factory, for this I will need some time, for example:

http://wtforms.simplecodes.com/docs/1.0.2/specific_problems.html

https://groups.google.com/forum/?fromgroups=#!topic/wtforms/cJl3aqzZieA

+4
source share
2 answers

Just add the appropriate fields to the base form at run time. Here's a sketch of how you can do this (albeit greatly simplified):

 class BaseSurveyForm(Form): # define your base fields here def show_survey(survey_id): survey_information = get_survey_info(survey_id) class SurveyInstance(BaseSurveyForm): pass for question in survey_information: field = generate_field_for_question(question) setattr(SurveyInstanceForm, question.backend_name, field) form = SurveyInstanceForm(request.form) # Do whatever you need to with form here def generate_field_for_question(question): if question.type == "truefalse": return BooleanField(question.text) elif question.type == "date": return DateField(question.text) else: return TextField(question.text) 
+4
source
 class BaseForm(Form): @classmethod def append_field(cls, name, field): setattr(cls, name, field) return cls from forms import TestForm form = TestForm.append_field("do_you_want_fries_with_that",BooleanField('fries'))(obj=db_populate_object) 

I use the extended BaseForm class for all my forms and have a convenient append_field function in the class.

Returns a class with an added field, because instances (form fields) cannot add fields.

0
source

Source: https://habr.com/ru/post/1433382/


All Articles