I'm trying to create a simple unique username function for use in a Formencode schema. Here is the function:
class UniqueUsername(formencode.FancyValidator):
def _to_python(self, value, state):
user = DBSession.query(User.user_name).filter(User.username==value)
if user is not None:
raise formencode.Invalid(
'That username already exists', value, state)
return value
The problem is that the query gets generated but never actually hits the database. The user variable simply contains the generated query, not the query results. How do I go about fixing this? Thanks so much.
It should be:
user = DBSession.query(User.user_name).filter(User.username==value).first()
also: is it User.user_name or User.username ?