views:

56

answers:

1

Hi, 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.

+4  A: 

It should be:

user = DBSession.query(User.user_name).filter(User.username==value).first()

also: is it User.user_name or User.username ?

zeemonkee
thanks for the quick answer - could you point me to the place in the docs that explains this behavior? Thanks again.
Marc
http://www.sqlalchemy.org/docs/05/reference/orm/query.html#sqlalchemy.orm.query.Query.first
zeemonkee