Hello!
To empty database table, I use this SQL Query:
TRUNCATE TABLE `books`
How to I Truncate table using Django models and orm?
I've tried this, but it doesn't work:
Book.objects.truncate()
Hello!
To empty database table, I use this SQL Query:
TRUNCATE TABLE `books`
How to I Truncate table using Django models and orm?
I've tried this, but it doesn't work:
Book.objects.truncate()
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.
You can do this in fast and lightweight way but not using Django ORM. You may execute raw SQL with Django connection cursor:
from django.db import connection
cursor = connection.cursor()
cursor.execute("TRUNCATE TABLE `books`")