wtforms-sqlalchemy icon indicating copy to clipboard operation
wtforms-sqlalchemy copied to clipboard

Add new coerce argument to wtforms_sqlalchemy.fields.QuerySelectField.

Open tsuyukimakoto opened this issue 10 years ago • 2 comments

Make QuerySelectField's selected works with model instance.

tsuyukimakoto avatar Jul 05 '15 02:07 tsuyukimakoto

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?

Henni avatar Feb 24 '17 18:02 Henni

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)

davidism avatar Jun 29 '18 15:06 davidism