Populating the options of a SingleSelectField directly from the
results of a SQLAlchemy query seems to be an intuitive and efficient
way to do things.  However, if I try this for a field with separate
value and contents, I get confusing "no validator specified" errors.

That's because, in SelectionField._extend_options in turbogears/
widgets/forms.py, the first option supplied is checked for being a
tuple or list.  The RowProxy object returned by SQLAlchemy looks like
a tuple, but it's not, so it fails this test and TG doesn't handle it.

For example,

opts = select([bags.c.bag_id, bags.c.type]).execute().fetchall()
bagType = SingleSelectField(options=opts)

doesn't work.  I can fix it by inserting
opts = [tuple(o) for o in opts]
...changing each returned RowProxy object by force, but that's icky
and impossible to guess.

This can be fixed by using duck typing in _extend_options.  I'm
considering submitting a patch among these lines.  Can anyone comment
on whether it's a good idea, and whether my programming looks OK?
I've tested it lightly on my own system and it seems to fix the
problem without introducing any new ones.

In SelectionField in turbogears/widgets/forms.py:

    def _extend_options(self, opts):
        if (len(opts) > 0) and not isTupleLike(opts[0]):  # this line
changed
        # was: if (len(opts) > 0) and not isinstance(opts[0],
(tuple,list)):
            new_opts = []
            for opt in opts:
                new_opts.append((opt,opt))
            return new_opts
        return opts

Just before the definition of SelectionField:

def isTupleLike(obj):
    try:
        obj[0], obj[1]
        if hasattr(obj, 'capitalize'):
            return False # no strings, Unicode, etc.
        return True
    except:
        return False

Thanks for your comments!
- Catherine
http://catherinedevlin.blogspot.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to