How to remove all of the data in a table using Django

zjm1126 picture zjm1126 · Dec 26, 2010 · Viewed 137k times · Source

I have two questions:

  1. How do I delete a table in Django?
  2. How do I remove all the data in the table?

This is my code, which is not successful:

Reporter.objects.delete()

Answer

Tiago picture Tiago · Dec 26, 2010

Inside a manager:

def delete_everything(self):
    Reporter.objects.all().delete()

def drop_table(self):
    cursor = connection.cursor()
    table_name = self.model._meta.db_table
    sql = "DROP TABLE %s;" % (table_name, )
    cursor.execute(sql)