Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new coerce argument to wtforms_sqlalchemy.fields.QuerySelectField. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tsuyukimakoto
Copy link

Make QuerySelectField's selected works with model instance.

@Henni
Copy link

Henni commented Feb 24, 2017

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?

@davidism
Copy link
Member

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)

mx-moth pushed a commit to mx-moth/wtforms-sqlalchemy that referenced this pull request Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants