跳转至

删除

delete

调用模型的 delete 方法可以删除模型。

    user = await User(name="Paimon").insert()
    await user.delete()

delete_many

使用模型类的 delete_many 来同时删除多条数据:

1
2
3
4
5
    user1 = await User(name="user 1").insert()
    user2 = await User(name="user 2").insert()
    user3 = await User(name="user 3").insert()

    await User.delete_many(user1, user2, user3)

完整代码

本章完整示例代码
import cherry

db = cherry.Database("sqlite+aiosqlite:///:memory:")


class User(cherry.Model):
    id: cherry.AutoIntPK = None
    name: str

    cherry_config = cherry.CherryConfig(tablename="user", database=db)


async def main():
    await db.init()

    user = await User(name="Paimon").insert()
    await user.delete()

    user1 = await User(name="user 1").insert()
    user2 = await User(name="user 2").insert()
    user3 = await User(name="user 3").insert()

    await User.delete_many(user1, user2, user3)


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())