I have two questions :
- how to delete the table in django
- how to remove all of the data in table
this is my code, but not successful :
Reporter.objects.delete()
I have two questions :
this is my code, but not successful :
Reporter.objects.delete()
Django 1.11 delete all objects from a database table -
Refer the Official Django documentation here as quoted below - https://docs.djangoproject.com/en/1.11/topics/db/queries/#deleting-objects
Note that delete() is the only QuerySet method that is not exposed on a Manager itself. This is a safety mechanism to prevent you from accidentally requesting Entry.objects.delete(), and deleting all the entries. If you do want to delete all the objects, then you have to explicitly request a complete query set:
I myself tried the code snippet seen below within my
somefilename.py
and within my
views.py
i have a view that simply renders a html page ...it ended deleting all entries from - model == model_4 , but now i get to see a Error screen within Admin console when i try to asceratin that all objects of model_4 have been deleted ...
Do consider that - if we do not go to the ADMIN Console and try and see objects of the model - which have been already deleted - the Django app works just as intended.
django admin screencapture
If you want to remove all the data from all your tables, you might want to try the command
python manage.py flush
. This will delete all of the data in your tables, but the tables themselves will still exist.See more here: https://docs.djangoproject.com/en/1.8/ref/django-admin/
There are a couple of ways:
To delete it directly:
To delete it from an instance:
// don't use same name
As per the latest documentation, the correct method to call would be:
Inside a manager: