Wtforms Forms, Syntax for Using Inline Formfield

Trying to fill out a Wtform form Wtform , with the data pulled out the mongo db database and then provided it with jinja / flask to create an editable pre-filled form for the REST system I am creating.

My form structure:

 class ProjectForm(Form): name = TextField("Name of Project") workflow =FieldList(TextField(""), min_entries=5) class InstituteForm(Form): institue_name = TextField("Name of Institue") email = FieldList(TextField(""), min_entries=3) project_name = FormField(ProjectForm) submit = SubmitField("Send")` 

I can pre-populate the list of fields using this syntax:

 form = InstituteForm(institue_name="cambridge", email=[" email@gmail ", " email@gmail "]) 

however, I cannot understand the syntax for pre-populating a FormField containing a form object.

First I create a project form:

 p = ProjectForm(name=" test", workflow=["adadadad", "adasdasd", "adasdadas"]) 

& now I'm trying to add it to the InstituteForm form.

I tried:

 form = InstituteForm(institue_name=store_i, project_name=p, email=store_email) 

for which I get the html output:

Loaded example output [ http://tinypic.com/r/jpfz9l/5] , there are not enough points to place the image for.

and I tried syntax like:

 form = InstituteForm(institue_name=store_i, project_name.name=p, email=store_email) 

and

 form = InstituteForm(institue_name=store_i, project_name=p.name, email=store_email) 

and even

 form = InstituteForm(institue_name=store_i, project_name=ProjectForm(name="this is a test"), email=store_email) 

Was there a search and found another thread (no answer) to a similar question:

Using FieldList and FormField

+4
source share
1 answer

There is a project_name can be a dict or object (do not form an object, because it will fill InstituteForm.project_name using the values ​​of the html tag), so you can use the following code:

 form = InstituteForm(institue_name="cambridge", project_name=dict(name="test name"), email=[" email@gmail ", " email@gmail "]) 

or

 class Project(object): name = "test" workflow = ["test1", "test2"] form = InstituteForm(institue_name="cambridge", project_name=Project(), email=[" email@gmail ", " email@gmail "]) 

or

 class Project(object): name = "test" workflow = ["test1", "test2"] class Institute(object): institue_name = "cambridge" project_name = Project() email = [" email@gmail ", " email@gmail "] form = InstituteForm(obj=Institute()) 

These examples are equivalent because WTForms used a constructor with the obj and **kwargs parameters, which work similarly for these examples.

+2
source

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


All Articles