Turbogears2 + ToscaWidgets2: question about tw2.sqla

68 views
Skip to first unread message

Pablo Carracedo García

unread,
Aug 14, 2012, 3:49:36 PM8/14/12
to toscawidge...@googlegroups.com
Hi all!

I'm writing an application, in fact a widget based on tw2.sqla.DbFormPage to connect directly to a table. The problem is that I want to assign the primary key with the form. The only way it's working is defining the primary key as Hidden in this way:

id            = tw2.forms.HiddenField()

I want id to be also editable before submiting, but defining the field as:

id            = tw2.forms.TextField()

I get:

Exception: cannot create with pk

Any help will be appreciated.

Best regards

Pablo


Paul Johnston

unread,
Aug 15, 2012, 5:16:23 AM8/15/12
to toscawidge...@googlegroups.com
Hi,

This is a use case I did not anticipate. At the moment DbFormPage looks to see if the primary key field is specified. If it is, it updates an existing record; otherwise it creates a new record.

I'm guessing in your case you want it to always create a record, and include the primary key. You probably also want a validator on the primary key field, to warn a user if the primary key is already taken.

I think for now you should write "CreatingDbFormPage" which is mostly a copy and paste of DbFormPage, but change this:

        utils.update_or_create(cls.entity, data,
                               protect_prm_tamp=protect_prm_tamp)

to:

        record = cls.entity()
        record.query.session.add(record)
        tw2.sqla.utils.from_dict(record, data)

If that works for you, then we can see about adding this to tw2.sqla - either as a separate widget, or an option on DbFormPage.

Paul




Pablo


--
You received this message because you are subscribed to the Google Groups "ToscaWidgets-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/toscawidgets-discuss/-/_cZQ9kxITxUJ.
To post to this group, send email to toscawidge...@googlegroups.com.
To unsubscribe from this group, send email to toscawidgets-dis...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/toscawidgets-discuss?hl=en.

Reply all
Reply to author
Forward
0 new messages