Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

SingleSelectField - dynamic content from DB at run time - TW2

106 views
Skip to first unread message

alonp

unread,
Jul 7, 2013, 8:02:11 AM7/7/13
to toscawidge...@googlegroups.com

Hello
I want to create a form with a SingleSelectField that will be dynamically populated every time the form is called.

Here is my form

class RoomForm(twf.TableForm):
    id = twf.HiddenField()

    name = twf.TextField(lvalidator=twc.Validator(required=True))
    description = twf.TextArea()

    building = twf.SingleSelectField(
        prompt_text=None,
        options = [(b.id,b.name) for b in DBSession.query(Building).all()]
    )

    action = 'saveNewRoom'
    buttons = [
        twf.SubmitButton()
    ]

That works fine, but options are generated only once, during Init (as far as i understand). and I want the options to change every time the form is loaded.

I've tried a few things but they didn't work, and I don't understand how prepare() works.

Thanks for the help!

Paul Johnston

unread,
Jul 7, 2013, 1:06:43 PM7/7/13
to toscawidge...@googlegroups.com
Hi,

Use tw2.sqla - it has DbSingleSelectField which does exactly what you want.

Paul

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "ToscaWidgets-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to toscawidgets-dis...@googlegroups.com.
To post to this group, send email to toscawidge...@googlegroups.com.
Visit this group at http://groups.google.com/group/toscawidgets-discuss.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

alonp

unread,
Jul 8, 2013, 6:19:39 AM7/8/13
to toscawidge...@googlegroups.com

Thanks, I've tried that, after seeing tw2.sqla being recommended here somewhere...

My problem with that is that I didn't manage to get it to work

I don't understand this line, in the docs:

To set the query property to use ZopeTransactionExtension, appropriate code must be added to your model.

What exactly do I need to add to my model?

Paul Johnston

unread,
Jul 9, 2013, 4:36:44 AM7/9/13
to toscawidge...@googlegroups.com
Hi,

It depends on the framework you're using. Is it Pyramid? Pylons? tw2 standalone? And are you using plain SQLAlchemy? Elixir? Declarative base?

A lot of these already have what you need built in. For the latest version of TurboGears, you need to uncomment this line in model/__init__.py:

# DeclarativeBase.query = DBSession.query_property()

Paul




--

alonp

unread,
Jul 9, 2013, 8:32:15 AM7/9/13
to toscawidge...@googlegroups.com

Thanks Paul
I am using Turbogears 2.2 framework.
I uncommented that line:
     DeclarativeBase.query = DBSession.query_property()

I am able can load data in to "DbSingleSelectField"
But i still need guidance.. Sorry for all the questions, I just don't get it quite yet.
I added this line to my form

    test = tws.DbSingleSelectField(entity=Building)

How do I decide which field to show? Currently I'm seeing the options as objects:

a list of:  <Building object at 0x0000000005B94550>

Paul Johnston

unread,
Jul 9, 2013, 9:13:10 AM7/9/13
to toscawidge...@googlegroups.com
Hi,

You need to add a __unicode__ method to your Building object, which returns what you want the user to see.

You're doing well... there's no tutorial for this at the moment, but there should be!

Paul




--

alonp

unread,
Jul 10, 2013, 3:43:15 AM7/10/13
to toscawidge...@googlegroups.com

Thank you very much! I got it working, here are the important pieces of code, to summarize, and maybe help others

I'm using TurboGears2.2 and tw2.sqla, I wanted to create a form for creating new rooms objects, and associate it with a building (from a list of building from Database). Here is what I did:

 

On "model/__init__.py"

Uncomment this line (might be line 22)

DeclarativeBase.query = DBSession.query_property()

 

Here is my model:
I've added " __unicode__" method to return the field I want to view in my template:

class Building(DeclarativeBase):

    __tablename__ = 'buildings'

    id = Column(Integer, autoincrement=True, primary_key=True)

    name = Column(Unicode(255))

    address =Column(Unicode(255))

 

    def __unicode__(self):

        return self.name

 

Here is my form:

import tw2.forms as twf

import tw2.sqla as tws

 

class RoomForm(twf.TableForm):

    id = twf.HiddenField()

    name = twf.TextField(validator=twc.Validator(required=True))

    description = twf.TextArea()

    building = tws.DbSingleSelectField(entity=Building, prompt_text=None,)

 

    action = 'saveNewRoom'

    buttons = [twf.SubmitButton()]

 

And here is my save method:

@validate(form=RoomForm,error_handler=room_form_error)

@expose()

    def saveNewRoom(self,**kw):

        room = Room()

        room.name = kw['name']

        room.description = kw['description']

        room.building_id = kw['building'].id

        DBSession.add(room)

        redirect('/manage/buildings')

Paul Johnston

unread,
Jul 10, 2013, 7:24:47 AM7/10/13
to toscawidge...@googlegroups.com
Hi,

Thanks for the summary.

Your save method can be made simpler - DBSession.add(Room(**kw)) You can actually make the whole new/load/save setup much simpler by using DbFormPage in tw2.sqla. However, that is moving away from the TurboGears model, and towards using tw2 standalone.

Paul



--

Moritz Schlarb

unread,
Jul 10, 2013, 8:20:07 AM7/10/13
to toscawidge...@googlegroups.com
And additionally, for TurboGears there's tgext.crud and sprox which allow you to create the whole CRUD interface based on your model descriptions with very few lines of code.
To unsubscribe from this group and stop receiving emails from it, send an email to toscawidgets-discuss+unsub...@googlegroups.com.

alonp

unread,
Jul 11, 2013, 4:54:38 AM7/11/13
to toscawidge...@googlegroups.com

Thanks for the advices guys,

About tgext.crud, I tried that before but had to do a lot of work, to get the template to look good (I'm building in Hebrew – RTL). But thanks anyway, maybe I'll open a different post about that.

Anyway ToscaWidgets2 are great! (my 2nd turbogears project with them)

matthew lange

unread,
Jul 10, 2013, 11:46:15 PM7/10/13
to toscawidge...@googlegroups.com
I don't want to hijack the thread--but this discussion bears the question--which db-enabled flavor of TW to use, i.e. TW standalone, TurboGears, or Pyramid (and perhaps others?).  Is there a document somewhere that describes the various strengths/weaknesses/appropriate uses of each?

To unsubscribe from this group and stop receiving emails from it, send an email to toscawidgets-dis...@googlegroups.com.

Moritz Schlarb

unread,
Jul 11, 2013, 3:20:36 PM7/11/13
to toscawidge...@googlegroups.com
Well ok, then you will have to replace the whole templates anyway, I see...

But there are some helpers to assist you, e.g. override_template: https://github.com/TurboGears/tgext.pluggable#replacing-templates ;)

Craig Small

unread,
Jul 15, 2013, 8:57:58 AM7/15/13
to toscawidge...@googlegroups.com
On Wed, Jul 10, 2013 at 08:46:15PM -0700, matthew lange wrote:
> I don't want to hijack the thread--but this discussion bears the
> question--which db-enabled flavor of TW to use, i.e. TW standalone,
> TurboGears, or Pyramid (and perhaps others?). Is there a document
> somewhere that describes the various strengths/weaknesses/appropriate uses
> of each?
Not sure, but I use TW2 and TG2. I've basically hijacked all the
widgets so most of them do json queries, roughly along the way sprox
does it but fixed some of the bugs it has.

I'm still not 100% sure its the right way, but it works for me. For
example, the suggestion to use the sqla widget might mean doing
"oversized" queries, ie querying all the columns and then using one.
However I don't fully get the magic python has with some of these
setups.

I don't use TG2 just to help me get to the database though, so I think
the answer to your question depends on what other stuff you need.

- Craig
--
Craig Small VK2XLZ http://enc.com.au/ csmall at : enc.com.au
Debian GNU/Linux http://www.debian.org/ csmall at : debian.org
GPG fingerprint: 5D2F B320 B825 D939 04D2 0519 3938 F96B DF50 FEA5

matthew lange

unread,
Jul 23, 2013, 3:37:16 AM7/23/13
to toscawidge...@googlegroups.com
I don't use TG2 just to help me get to the database though, so I think
the answer to your question depends on what other stuff you need.

Right, which is why I asked about "appropriate uses".
I am sure that some of these technologies have better strengths/weaknesses for specific use cases than others.
It'd be awesome if someone who knew this domain well wrote that up in a primer, perhaps even a feature matrix.

~mc

Paul Johnston

unread,
Jul 23, 2013, 4:34:17 AM7/23/13
to toscawidge...@googlegroups.com


--
Reply all
Reply to author
Forward
0 new messages