How to populate deform select widgets from foreign key and enum columns in a NICE way

483 views Asked by At

I've been wrestling with deform, sqlalchemy and colanderalchemy for a little while now and I'm having some trouble with populating select boxes.

Question 1

Is there a better way to do this:

value_type = Column(Enum('string','boolean','integer','float','reference','enum'),info={
                                'colanderalchemy':
                                {
                                    'widget':deform.widget.SelectWidget(
                                        values = [
                                                    ('string','string'),
                                                    ('boolean','boolean'),
                                                    ('integer','integer'),
                                                    ('float','float'),
                                                    ('reference','reference'),
                                                    ('enum','enum')
                                                ]
                                    )
                                }})

Question2:

And how about this:

reference_cat_id = Column(Integer,ForeignKey('category_nodes.id'),info={'colanderalchemy':{'widget':deform.widget.SelectWidget()}})
...

n = SQLAlchemySchemaNode(MyModel)
n.children[x].widget.values = [
                        (
                            oCat.id,
                            oCat.name
                        )
                        for oCat in
                        DBSession.query(CategoryNode).filter_by(accepted=True)
                        if oCat.getType() == 'Base'
                ] 
1

There are 1 answers

0
ting12 On

The 2nd example may be more relevant if the 'n.children[x]' syntax could ever be overriden (not sure the x integer would be a good way to point to the right column when subclassing the whole script) and the column name passed instead.