Paul's Programming Notes     Archive     Feed     Github

AttributeError: 'QuerySelectField' object has no attribute '_sa_instance_state'

I came across this error when I was trying to make a custom validator in Flask-Admin with wtforms form objects.

This code didn't work:
def location_must_not_conflict(form, field):
if Event.query.filter(Event.location == form.location).first():
raise wtforms.validators.ValidationError('Location conflicts with another request for the same venue.')

Because I was using form.location and not form.location.data.

This will work:
def location_must_not_conflict(form, field):
if Event.query.filter(Event.location == form.location.data).first():
raise wtforms.validators.ValidationError('Location conflicts with another request for the same venue.')