Saturday, May 24, 2014

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.')

No comments:

Post a Comment