Add data atrributes to option via WTForms

Viewed 60

I have form where I set objects label (using WTforms-SQLAlchemy):

label = QuerySelectField(query_factory=labels)

As I have my own styling of Select field using javascript, I would like to pass value of label.color to my HTML.
My idea is to have <option data-color="mycolor" ...></option>.
How (if) can I achieve this?

Edit:
As I look into WTForms SelectFieldBase source code, it doesn't seem possible:

def __iter__(self):
        opts = dict(widget=self.option_widget, _name=self.name, _form=None, _meta=self.meta)
        for i, (value, label, checked) in enumerate(self.iter_choices()):
            opt = self._Option(label=label, id='%s-%d' % (self.id, i), **opts)
            opt.process(None, value)
            opt.checked = checked
            yield opt

I looks like iterator only uses value, label and checked, so any other values are lost. Am I reading it right?

The same is in WTForms-SQLAlchemy SelectQueryField, where we get:

    def iter_choices(self):
        if self.allow_blank:
            yield ('__None', self.blank_text, self.data is None)

        for pk, obj in self._get_object_list():
            yield (pk, self.get_label(obj), obj == self.data) 
0 Answers
Related