3

The deform select widget takes a sequence of two element tuples. How do I create two element tuples from two columns from an sqlalchemy query.

The below code works with the hardcoded example.

class ProfileValueSelect(colander.MappingSchema):
    choices = (
        ('', '- Select -'),
        ('one', 'One'),
        ('two', 'Two'),
        ('three', 'Three')
    )
    menu = colander.SchemaNode(
        colander.String(),
        title=False,
        missing=unicode(''),
        widget=deform.widget.SelectWidget(values=choices)
    )

Didn't supply enough information, but found the solution to be more straight forward than I thought.

class ProfileValueSelect(colander.MappingSchema):
    result = DBSession.query(Profile.uid, Profile.value).order_by(Profile.value).all()
    menu = colander.SchemaNode(
    colander.String(),
    title=False,
    missing=unicode(''),
    widget=deform.widget.SelectWidget(values=result)
)
BScott
  • 135
  • 6
  • how do you get data from sqlalchemy? – Paweł Kordowski Jan 27 '16 at 20:05
  • Take a look here https://github.com/websauna/websauna/blob/master/websauna/system/user/schemas.py#L49 and here https://github.com/websauna/websauna/blob/master/websauna/system/form/sqlalchemy.py#L219 - I hope to cover this in standalone tutorial in some point of future. If you don't have answer yet I can do it now. – Mikko Ohtamaa Feb 12 '16 at 22:28
  • You should supply the solution as an answer, even if you came up with it yourself. – einSelbst Mar 27 '18 at 08:14

0 Answers0