217π
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)
146π
As per the latest documentation, the correct method to call would be:
Reporter.objects.all().delete()
- [Django]-Django β Annotate multiple fields from a Subquery
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-Django migration strategy for renaming a model and relationship fields
52π
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/
- [Django]-Django: Redirect to previous page after login
- [Django]-How exactly do Django content types work?
- [Django]-Django return file over HttpResponse β file is not served correctly
40π
Using shell,
1) For Deleting the table:
python manage.py dbshell
>> DROP TABLE {app_name}_{model_name}
2) For removing all data from table:
python manage.py shell
>> from {app_name}.models import {model_name}
>> {model_name}.objects.all().delete()
- [Django]-Django optional URL parameters
- [Django]-Running Django with FastCGI or with mod_python
- [Django]-Naming convention for Django URL, templates, models and views
6π
Django 1.11 delete all objects from a database table β
Entry.objects.all().delete() ## Entry being Model Name.
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
# for deleting model objects
from django.db import connection
def del_model_4(self):
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(model_4)
and within my views.py
i have a view that simply renders a html page β¦
def data_del_4(request):
obj = calc_2() ##
obj.del_model_4()
return render(request, 'dc_dash/data_del_4.html') ##
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 β¦
ProgrammingError at /admin/dc_dash/model_4/
relation "dc_dash_model_4" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "dc_dash_model_4"
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]-How do you catch this exception?
- [Django]-Mixin common fields between serializers in Django Rest Framework
- [Django]-Django dynamic forms β on-the-fly field population?
5π
Use this syntax to delete the rows also to redirect to the homepage (To avoid page load errors) :
def delete_all(self):
Reporter.objects.all().delete()
return HttpResponseRedirect('/')
- [Django]-How do you change the collation type for a MySQL column?
- [Django]-Django: sqlite for dev, mysql for prod?
- [Django]-How do I run tests for all my Django apps only?
3π
You can use the Django-Truncate library to delete all data of a table without destroying the table structure.
Example:
- First, install django-turncate using your terminal/command line:
pip install django-truncate
- Add "django_truncate" to your INSTALLED_APPS in the
settings.py
file:
INSTALLED_APPS = [
...
'django_truncate',
]
- Use this command in your terminal to delete all data of the table from the app.
python manage.py truncate --apps app_name --models table_name
- [Django]-Charts in django Web Applications
- [Django]-Django storages aws s3 delete file from model record
- [Django]-Get count of related model efficiently in Django
2π
There are a couple of ways:
To delete it directly:
SomeModel.objects.filter(id=id).delete()
To delete it from an instance:
instance1 = SomeModel.objects.get(id=id)
instance1.delete()
// donβt use same name
- [Django]-How to pass multiple values for a single URL parameter?
- [Django]-How to specify an IP address with Django test client?
- [Django]-Django: Multiple forms possible when using FormView?
2π
I know the question is for a long time ago, but just for the record, because this saved me:
python manage.py flush
The above will delete all the data on your database (PostgreSQL in my case). Even the superuser.
If you want to delete all the rows of a specific table, then:
python manage.py shell
>> from app.models import SomeTable
>> SomeTable.objects.all().delete()
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Django. A good tutorial for Class Based Views
- [Django]-Add inline model to django admin site
-4π
Actually, I un-register the model (the table data that I want to delete) from the admin.py. Then I migrate.
python manage.py makemigrations
python manage.py migrate
python runserver
Then I register the model in the admin.py and do migration again. π Now, the table is empty. This might not be a professional answer, but it helped me.
- [Django]-How does the get_or_create function in Django return two values?
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-Parsing unicode input using python json.loads