Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.6: Clear data in one table

I've a table name UGC and would like to clear all the data inside that table. I don't want to reset the entire app which would delete all the data in all the other models as well. Is it possible to clear only one single model? I also have South configured with my app, if that would help.

like image 436
James L. Avatar asked Dec 04 '25 01:12

James L.


2 Answers

You could use raw SQL :

 cursor.execute(“DROP TABLE UGC”)

or you could just use the ORM directly inside a Django shell :

UGCModel.objects.all().delete()

That would erase the data (not the table, though), so you have to be careful :)

Another way (for completeness and to make use of South) would be to comment out the model in your models declaration, migrate and then put it back again (assuming there are no models with a reference to it.)

HTH

like image 158
Ambroise Avatar answered Dec 05 '25 15:12

Ambroise


In the admin interface, you can go to the list page for that model, then you can select all models and use the Delete selected ... action at the top of the table.

Remember that, in whatever way you delete the data, foreign keys default to ON DELETE CASCADE, so any model with a foreign key to a model you want to delete will be deleted as well. The admin interface will give you a complete overview of models that will be deleted.

like image 29
knbk Avatar answered Dec 05 '25 16:12

knbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!