wtforms-sqlalchemy
wtforms-sqlalchemy copied to clipboard
Add new coerce argument to wtforms_sqlalchemy.fields.QuerySelectField.
Make QuerySelectField's selected works with model instance.
I'd really like to get this in as I'm running into a similar issue as described in the test file. But I believe a simple implementation as for the normal SelectField would suffice:
yield (value, label, self.coerce(value) == self.data)
@crast @prencher is this project still alive?
Is coerce needed? pk == text_type(get_pk(obj)) should work and matches how comparisons already work.
I'm fine with adding a fallback comparing primary keys, but note that you can also solve this by implementing equality for your models.
from sqlalchemy import inspect
class _Base:
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return inspect(self).identity_key == inspect(other).identity_key
Base = declarative_base(cls=_Base)