如何使用peewee ORM在mysql表中找到用户的存在?

2024-10-02 16:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在mysql中有这个表

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")

它返回<User: zhaochang>

但对于不存在的随机电子邮件/用户User.get(email="some_random@email.com"),它会抛出错误:

UserDoesNotExist: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']

我在期待用户.get方法返回None。你知道吗


Tags: 用户selfcomgetemailusernamerandomsome