I am just getting started with TW, using Pylons. I have created a form
class - as an example, I have:
class AddCommentForm(TableForm):
class fields(WidgetsList):
id = HiddenField(default="I'm hidden!")
author = TextField(
validator = UnicodeString(not_empty=True),
)
... etc
This works great (using the process as described on the pylons site -
http://docs.pythonweb.org/display/pylonscookbook/An+Alternative+ToscaWidgets+Setup+with+Mako)
However, if I don't know which fields I need beforehand ("id",
"author", etc.), how would I create this class dynamically at run
time? None of the obvious ways seem to work...
Thanks!
David
There are various ways you could do this. One would be to dynamically
create a widgets subclass and instantiate it or a better alternative
would be to create a list with all the widgets and pass it to the
constructor. Something like:
fields = generate_fields(model.Something)
form = TableForm("a_form", fields=fields)
There's some work in this direction at TGFastData but it only handles
TG widgets and SQLObject. It should be fairly easy to adapt to TW
and it will probably be a useful addition to twForms since the
needed dependencies (RuleDispatch) are already required by TW. Here's
the source [1} in case you want some ideas (take a look at the
fields_for function)
Alberto
On Jun 11, 12:37 pm, Timothy Sweetser <transitauthor...@gmail.com>
wrote:
>
> Yup, definately. It's working right now. I'm just moving things around
> a bit and adding some different functionality. I'll probably post a
> link on here later today when I'm happy with it.
Cool. Looking forward to see it :)
Alberto
On Jun 14, 11:17 am, Timothy Sweetser <transitauthor...@gmail.com>
wrote:
-Josh
http://wistful.googlecode.com/files/fieldfactory.py
To use:
1. Toss the module somewhere in your project (lib/ with Pylons apps).
2. Import the generate_form_fields function and call it with your sqla
Mapper/Elixir Entity as a parameter. If you don't want it to guess
which fields are passwords, zip codes, email, etc, set the optional
apply_rules param to False.
3. It'll return a list of fields, which you can then drop into your
form, a la Alberto's example in the second message of the thread.
Let me know if it works, fails, or is messed up somewhere... Comments
and feedback are much appreciated. Hopefully with a little work and
some decent docstrings this can get integrated into TW.
It was designed with some code generation functionality, so I'm
planning on writing up some TG2/Pylons-compatible paster commands
that'll use it to do basic form scaffolding. Assuming the current code
isn't horrendously ill-conceived, those'll be next on my list.
On Jun 3, 5:31 pm, david <dgel...@gmail.com> wrote:
> Hi,
>
> I am just getting started with TW, using Pylons. I have created a form
> class - as an example, I have:
>
> class AddCommentForm(TableForm):
> class fields(WidgetsList):
> id = HiddenField(default="I'm hidden!")
> author = TextField(
> validator = UnicodeString(not_empty=True),
> )
> ... etc
>
> This works great (using the process as described on the pylons site -http://docs.pythonweb.org/display/pylonscookbook/An+Alternative+Tosca...)
On Jul 8, 10:23 pm, Timothy Sweetser <transitauthor...@gmail.com>
wrote:
On Jul 9, 12:26 pm, Timothy Sweetser <transitauthor...@gmail.com>
>
> Actually, in the interests of #5 on that list, the code that
> interfaces with the sqla column is getting refactored to make it more
> generic. Any comments so far?
Although I haven't actually run the code yet it looks very
interesting! Something like this could be a very useful addition to
twForms...
I'll let you know my thoughts as soon as I get a chance to give it a
try. Has anyone tried it out that can comment?
Thanks for sharing it!
Alberto
I've got a limited amount of time before our project has to go live, &
so I'm not confident enough to use this straight away, though I *was*
interested in how one might go about making any changes to the display
of form fields before they get displayed. At least when you explicitly
design a form, you can customise the appearance of individual fields...
(eg, to turn a foreign key integer data source into a select list)...
I am keeping my eye on this though - looks fantastic :)
I should be posting some improvements in the next few days.