librelist archives

« back to archive

Help with WTForm SelectField in Flask-WTF

Help with WTForm SelectField in Flask-WTF

From:
Terry Zhao
Date:
2011-07-22 @ 04:12
Hi, All

I used a SelectField of WTForm, and initially set a choices to it  like
this:

class MyForm(Form):
  category = SelectField(u"Category", choices=[(c.id, c.name) for c in
Category.query.order_by('-id')], coerce=int)

and in my view I use form = MyForm(request.form) to get my form, and set it
(Edit view) or just render it.

MY QUESTION is, whatever I update the Category model to DB,
such as add/update a new category data,
the SelectField will never refresh until the next Flask restart.

Is there any way to auto-refresh the choices data of SelectField ?

Any tips will be highly appreciated, Thanks in Advance.

-- 
Thanks & Best Regards.
赵慧 / Terry Zhao

Re: [flask] Help with WTForm SelectField in Flask-WTF

From:
Sean Lynch
Date:
2011-07-22 @ 04:17
I haven't tried this yet, but I think you might want to use
a wtforms.ext.sqlalchemy.fields.QuerySelectField
http://wtforms.simplecodes.com/docs/0.6.2/ext.html#module-wtforms.ext.sqlalchemy


On Fri, Jul 22, 2011 at 12:12 AM, Terry Zhao <huiyubird@gmail.com> wrote:

> Hi, All
>
> I used a SelectField of WTForm, and initially set a choices to it  like
> this:
>
> class MyForm(Form):
>   category = SelectField(u"Category", choices=[(c.id, c.name) for c in
> Category.query.order_by('-id')], coerce=int)
>
> and in my view I use form = MyForm(request.form) to get my form, and set it
> (Edit view) or just render it.
>
> MY QUESTION is, whatever I update the Category model to DB,
> such as add/update a new category data,
> the SelectField will never refresh until the next Flask restart.
>
> Is there any way to auto-refresh the choices data of SelectField ?
>
> Any tips will be highly appreciated, Thanks in Advance.
>
> --
> Thanks & Best Regards.
> 赵慧 / Terry Zhao
>
>

Re: [flask] Help with WTForm SelectField in Flask-WTF

From:
Terry Zhao
Date:
2011-07-22 @ 12:25
It works!
Thanks, Sean :-)

On Fri, Jul 22, 2011 at 12:17 PM, Sean Lynch <techniq35@gmail.com> wrote:

> I haven't tried this yet, but I think you might want to use
> a wtforms.ext.sqlalchemy.fields.QuerySelectField
>
> http://wtforms.simplecodes.com/docs/0.6.2/ext.html#module-wtforms.ext.sqlalchemy
>
>
> On Fri, Jul 22, 2011 at 12:12 AM, Terry Zhao <huiyubird@gmail.com> wrote:
>
>> Hi, All
>>
>> I used a SelectField of WTForm, and initially set a choices to it  like
>> this:
>>
>> class MyForm(Form):
>>   category = SelectField(u"Category", choices=[(c.id, c.name) for c in
>> Category.query.order_by('-id')], coerce=int)
>>
>> and in my view I use form = MyForm(request.form) to get my form, and set
>> it (Edit view) or just render it.
>>
>> MY QUESTION is, whatever I update the Category model to DB,
>> such as add/update a new category data,
>> the SelectField will never refresh until the next Flask restart.
>>
>> Is there any way to auto-refresh the choices data of SelectField ?
>>
>> Any tips will be highly appreciated, Thanks in Advance.
>>
>> --
>> Thanks & Best Regards.
>> 赵慧 / Terry Zhao
>>
>>
>


-- 
Thanks & Best Regards.
赵慧 / Terry Zhao

Re: [flask] Help with WTForm SelectField in Flask-WTF

From:
Sean Lynch
Date:
2011-07-22 @ 14:48
Great :)

Btw, you may want to look into using model_form to automatically create your
models (not sure if it handles the QuerySelectList for you, but you could
always override the produced from by passing in field_args for that field).

I wrote a snippet on how this works here -
http://flask.pocoo.org/snippets/60/

-Sean

On Fri, Jul 22, 2011 at 8:25 AM, Terry Zhao <huiyubird@gmail.com> wrote:

> It works!
> Thanks, Sean :-)
>
> On Fri, Jul 22, 2011 at 12:17 PM, Sean Lynch <techniq35@gmail.com> wrote:
>
>> I haven't tried this yet, but I think you might want to use
>> a wtforms.ext.sqlalchemy.fields.QuerySelectField
>>
>> 
http://wtforms.simplecodes.com/docs/0.6.2/ext.html#module-wtforms.ext.sqlalchemy
>>
>>
>> On Fri, Jul 22, 2011 at 12:12 AM, Terry Zhao <huiyubird@gmail.com> wrote:
>>
>>> Hi, All
>>>
>>> I used a SelectField of WTForm, and initially set a choices to it  like
>>> this:
>>>
>>> class MyForm(Form):
>>>   category = SelectField(u"Category", choices=[(c.id, c.name) for c in
>>> Category.query.order_by('-id')], coerce=int)
>>>
>>> and in my view I use form = MyForm(request.form) to get my form, and set
>>> it (Edit view) or just render it.
>>>
>>> MY QUESTION is, whatever I update the Category model to DB,
>>> such as add/update a new category data,
>>> the SelectField will never refresh until the next Flask restart.
>>>
>>> Is there any way to auto-refresh the choices data of SelectField ?
>>>
>>> Any tips will be highly appreciated, Thanks in Advance.
>>>
>>> --
>>> Thanks & Best Regards.
>>> 赵慧 / Terry Zhao
>>>
>>>
>>
>
>
> --
> Thanks & Best Regards.
> 赵慧 / Terry Zhao
>
>