Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created June 25, 2013 21:07
Show Gist options
  • Save doobeh/5862407 to your computer and use it in GitHub Desktop.
Save doobeh/5862407 to your computer and use it in GitHub Desktop.
Extended QuerySelectField to allow SQLAlchemy objects to be passed in as default avalues
class QuerySelectFieldCustom(QuerySelectField):
"""Extended to allow default checked items
Probably a much more obvious way to do this...
"""
selected = None
def iter_choices(self):
if self.allow_blank:
yield (u'__None', self.blank_text, self.data is None)
for pk, obj in self._get_object_list():
if self.selected is not None and self.selected.id == obj.id:
yield (pk, self.get_label(obj), True)
else:
yield (pk, self.get_label(obj), obj in self.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment