如何使用 peewee ORM 查找 mysql table 中存在的用户?
How to find of user exists in mysql table using peewee ORM?
我在 mysql、
中有这个 table
import peewee
class User(peewee.Model):
username = peewee.Charfield(max_length=60)
email = peewee.Charfield(max_length=300)
def __repr__(self):
return "<User: {}>".format(self.username)
当我为现有用户尝试以下代码时:
User.get(email="zhaochang@qq.com")
它returns <User: zhaochang>
但是对于不存在的随机 email/user User.get(email="some_random@email.com")
它会抛出错误:
用户不存在:Instance matching query does not exist:
SQL: SELECT 't1'.'id', 't1'.'email', 't1'.'username' FROM 'user' AS t1 WHERE ('t1'.'email' = %s)
PARAMS: [u'some_random@email.com']
我期待 User.get 方法 return None。
当文档明确指出
时,您为什么期望该方法 return None
If no model is returned, a DoesNotExist is raised.
来源:http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.get
我在 mysql、
中有这个 tableimport peewee
class User(peewee.Model):
username = peewee.Charfield(max_length=60)
email = peewee.Charfield(max_length=300)
def __repr__(self):
return "<User: {}>".format(self.username)
当我为现有用户尝试以下代码时:
User.get(email="zhaochang@qq.com")
它returns <User: zhaochang>
但是对于不存在的随机 email/user User.get(email="some_random@email.com")
它会抛出错误:
用户不存在:Instance matching query does not exist:
SQL: SELECT 't1'.'id', 't1'.'email', 't1'.'username' FROM 'user' AS t1 WHERE ('t1'.'email' = %s)
PARAMS: [u'some_random@email.com']
我期待 User.get 方法 return None。
当文档明确指出
时,您为什么期望该方法 return NoneIf no model is returned, a DoesNotExist is raised.
来源:http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.get