librelist archives

« back to archive

WTForms and SelectField

WTForms and SelectField

From:
Adam Oakman
Date:
2011-03-24 @ 19:35
This is my first SelectField with WTForms on Google App Engine,  
everything else has worked great. I have the field almost working,  
with data bound from a query but I am unable to set the default value.

My form is declared as:
class ItemsAddForm(Form):
     name = TextField(u'Item Name*', description="The item, style or  
product name.", validators=[validators.Required(),  
validators.length(max=200)])
     brand = SelectField(u'Brand*', description="What brand does this  
item belong to?", validators=[validators.Required()], coerce=int)

I'm declaring the form and binding the data like so:

         form = ItemsAddForm(request.form, item)
         if not form.brand.choices:
             form.brand.choices = [(b.key().id(), b.name) for b in  
Entity_Brands.all().filter('owner =', g.user.owner).order('name')]
             form.brand.default = item.brand.key().id()

I was under the impression that by setting the default should do the  
trick but no luck, it always defaulting to the first item. Has anyone  
done something like this?

BTW I also tried the WTForms GAE helper ReferencePropertyField but was  
getting the same result.

Thanks

A

Re: [flask] WTForms and SelectField

From:
Kates Gasis
Date:
2011-03-24 @ 23:06
Since you're setting a "default" value at runtime, why not just set the 
data directly. 

form.brand.data = item.brand.key.key().id()




On 25-Mar-2011, at 3:35 AM, Adam Oakman wrote:

> This is my first SelectField with WTForms on Google App Engine,  
> everything else has worked great. I have the field almost working,  
> with data bound from a query but I am unable to set the default value.
> 
> My form is declared as:
> class ItemsAddForm(Form):
>     name = TextField(u'Item Name*', description="The item, style or  
> product name.", validators=[validators.Required(),  
> validators.length(max=200)])
>     brand = SelectField(u'Brand*', description="What brand does this  
> item belong to?", validators=[validators.Required()], coerce=int)
> 
> I'm declaring the form and binding the data like so:
> 
>         form = ItemsAddForm(request.form, item)
>         if not form.brand.choices:
>             form.brand.choices = [(b.key().id(), b.name) for b in  
> Entity_Brands.all().filter('owner =', g.user.owner).order('name')]
>             form.brand.default = item.brand.key().id()
> 
> I was under the impression that by setting the default should do the  
> trick but no luck, it always defaulting to the first item. Has anyone  
> done something like this?
> 
> BTW I also tried the WTForms GAE helper ReferencePropertyField but was  
> getting the same result.
> 
> Thanks
> 
> A
> 

Re: [flask] WTForms and SelectField

From:
Adam Oakman
Date:
2011-03-24 @ 23:12
Wow, that works, now I just need to understand why that works and the  
default didn't. Thanks heaps!



On Mar 24, 2011, at 6:06 PM, Kates Gasis wrote:

>
> Since you're setting a "default" value at runtime, why not just set  
> the data directly.
>
> form.brand.data = item.brand.key.key().id()
>
>
>
>
> On 25-Mar-2011, at 3:35 AM, Adam Oakman wrote:
>
>> This is my first SelectField with WTForms on Google App Engine,
>> everything else has worked great. I have the field almost working,
>> with data bound from a query but I am unable to set the default  
>> value.
>>
>> My form is declared as:
>> class ItemsAddForm(Form):
>>    name = TextField(u'Item Name*', description="The item, style or
>> product name.", validators=[validators.Required(),
>> validators.length(max=200)])
>>    brand = SelectField(u'Brand*', description="What brand does this
>> item belong to?", validators=[validators.Required()], coerce=int)
>>
>> I'm declaring the form and binding the data like so:
>>
>>        form = ItemsAddForm(request.form, item)
>>        if not form.brand.choices:
>>            form.brand.choices = [(b.key().id(), b.name) for b in
>> Entity_Brands.all().filter('owner =', g.user.owner).order('name')]
>>            form.brand.default = item.brand.key().id()
>>
>> I was under the impression that by setting the default should do the
>> trick but no luck, it always defaulting to the first item. Has anyone
>> done something like this?
>>
>> BTW I also tried the WTForms GAE helper ReferencePropertyField but  
>> was
>> getting the same result.
>>
>> Thanks
>>
>> A
>>
>