Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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() 
like image 824
zjm1126 Avatar asked Dec 26 '10 03:12

zjm1126


People also ask

How do I delete items in Django?

Delete a Record of a Model Using the delete() Method in Django. All the model objects or instances in Django have a delete() method, which can be used to delete that record. Now, this delete() method can be also be used to delete a single record and a bunch of them.

What does Django DB models model clean () do?

This method will validate all fields on your model. The optional exclude argument lets you provide a set of field names to exclude from validation. It will raise a ValidationError if any fields fail validation. The second step full_clean() performs is to call Model.clean() .

What is objects all () Django?

The Manager is the main source of QuerySets for a model. For example, Blog.objects.all() returns a QuerySet that contains all Blog objects in the database.


2 Answers

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) 
like image 95
Tiago Avatar answered Oct 13 '22 10:10

Tiago


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

Reporter.objects.all().delete() 
like image 20
Ash Avatar answered Oct 13 '22 10:10

Ash