How to TRUNCATE TABLE using Django's ORM?

后端 未结 8 1008
别跟我提以往
别跟我提以往 2020-12-15 15:28

To empty a database table, I use this SQL Query:

TRUNCATE TABLE `books`

How to I truncate a table using Django\'s models and ORM?

I

8条回答
  •  别那么骄傲
    2020-12-15 16:17

    The closest you'll get with the ORM is Book.objects.all().delete().

    There are differences though: truncate will likely be faster, but the ORM will also chase down foreign key references and delete objects in other tables.

提交回复
热议问题