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


Question

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()
1
95
5/4/2019 10:43:57 PM

Accepted Answer

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)
109
12/26/2010 3:58:47 AM

As per the latest documentation, the correct method to call would be:

Reporter.objects.all().delete()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon