Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wtforms Forms, Syntax for acessing embedded Formfield

Trying to populate a Wtform form field, with data pulled out a mongo db database, then giving it to jinja/flask to create a editable pre-populated form for a REST system I'm building.

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 my field list using this syntax:

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

however, I cannot figure out 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 am trying to add it to the InstituteForm form.

I have tried:

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

for which I get html output:

Uploaded example output [http://tinypic.com/r/jpfz9l/5], do not have enough points to post an image to stack overflow.

and I have 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)

Did search and found another thread (with no reply) to a similar question:

Using FieldList and FormField

like image 453
Mr Gallagher Avatar asked Oct 22 '22 06:10

Mr Gallagher


1 Answers

There are project_name can be dict or object (not form object because it will populate InstituteForm.project_name with html tag values), so you can use next 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())

This examples equivalent because WTForms used constructor with obj parameters and **kwargs which work similar for this examples.

like image 105
tbicr Avatar answered Oct 24 '22 03:10

tbicr