Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Compose form at runtime?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jim Jones  
View profile  
 More options Feb 7 2009, 12:52 pm
From: Jim Jones <jjimjjo...@googlemail.com>
Date: Sat, 7 Feb 2009 18:52:42 +0100
Local: Sat, Feb 7 2009 12:52 pm
Subject: Compose form at runtime?
Hello everybody,

is there a way to dynamically assemble a form at runtime?
I'm thinking along the lines of:

myform = Form()
myform.append( TextField('Username') )
myform.append( TextField('Email') )
myform.append( TextField('Foo') )

This would be helpful for CRUD-style applications where
not all fields are previously known.

regards
-jj


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Crasta  
View profile  
 More options Feb 7 2009, 2:03 pm
From: James Crasta <jcra...@gmail.com>
Date: Sat, 7 Feb 2009 14:03:07 -0500
Local: Sat, Feb 7 2009 2:03 pm
Subject: Re: [WTForms] Compose form at runtime?

On Sat, Feb 7, 2009 at 12:52 PM, Jim Jones <jjimjjo...@googlemail.com> wrote:
> is there a way to dynamically assemble a form at runtime?

This is possible by creating a subclass of Form inside your view function:

def edit(request):
    class F(Form):
        pass
    F.username = TextField('username')
    for name in iterate_some_model_dynamically():
        setattr(F, name, TextField(name.title()))

    form = F(request.POST, obj=mymodel)
    # do view stuff

Note that we create a Form subclass inside the view function, thus if the
form can change between requests, you won't get an issue with conflicting
attributes and thread-safety. If the form can be generated once at
application startup, then you can simply move your form creation to module
level, or use a factory type approach (see model_form in wtforms.ext.django
for inspiration there.) For example:

def my_form_factory(model):
    F = type(type(model).__name__ + 'Form', (Form, ), {})
    for name in dir(model):
       field = getattr(model, name)
       if type(field).__name__ == 'CharField':
           setattr(F, name, TextField(field.label))

UserForm = my_form_factory(models.User)

The reason we don't support adding fields directly on to form _instances_,
only onto form classes is that we pass the input data to the form at
instantiation time. This means that at instantiation time the form needs to
know all its fields. It also semi-implies the contract where the form is
bound to one set of data per instantiation.

It should be noted that since forms can subclass other forms, you can mix
together dynamic forms and 'hardcoded' forms in any order you wish:

class CsrfForm(Form):
    csrf_token = HiddenField()

class UserForm(CsrfForm):
    pass

for x in dir(User):
    setattr(UserForm, x, TextField(x))

class UserProfileForm(UserForm):
    bio = TextAreaField("User Biography")

There's a lot of possibilities here, depending on what you need to do. If
you are designing automagic CRUD forms for some ORM framework other than
django, let us know, we might be interested in co-opting it as an extension.
We already have planned for 0.4 a SQLAlchemy form factory similar to the
django version as well.  Hope I was of some help.

James


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Jones  
View profile  
 More options Feb 7 2009, 4:36 pm
From: Jim Jones <jjimjjo...@googlemail.com>
Date: Sat, 7 Feb 2009 22:36:41 +0100
Local: Sat, Feb 7 2009 4:36 pm
Subject: Re: [WTForms] Re: Compose form at runtime?

Thank you for the detailed explanation.
That's just perfect, exactly what I need.

> There's a lot of possibilities here, depending on what you need to
> do. If you are designing automagic CRUD forms for some ORM framework
> other than django, let us know, we might be interested in co-opting
> it as an extension. We already have planned for 0.4 a SQLAlchemy form
> factory similar to the django version as well.  Hope I was of some
> help.

I'm indeed using SQLAlchemy here but the planned forms magic will
not be directly tied to the model. The custom objects that my app
allows to create are saved to a generic model, so a direct 1:1 mapping
- while definately interesting for other purposes - is not my use-case.

Nonetheless thanks for your help and keep up the good work!

best regards
-jj


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »