Django 1.6: Clear data in one table

Question:

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.

Asked By: James L.

||

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

Answered By: 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.

Answered By: knbk

Faced such issues today with django 2.0.2 because i created my model with

class Front(models.Model):
    pass

and migrated it but when i later updated the model, i couldn’t run
python manage.py makemigrations because it was saying

You are trying to add a non-nullable field 'update' to front without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows 
with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option:

What was my solution?

  1. I choose option 2 which is to quit
  2. I commented out the troublesome model/table which is Front in my case
  3. I ran python manage.py makemigrations which deleted the troublesome table/model
  4. I uncommented my model and ran python manage.py makemigrations again which recreated the table/model and finally
  5. I migrated my changes with python manage.py migrate and everyhing was resolved!

Note: Be careful with the above instruction cause it will delete all references/foreign keys to the table/model with on_delete=models.CASCADE which is the default!

Django 3.1.14

If you’re interested in doing it on the command line, and you’ll be doing this type of cleaning of your db frequently, I do this:

# project_name/app_name/management/commands/clear_test_models.py

from django.core.management.base import BaseCommand
from django.apps import apps

keep_models = ['KeepModel0', 'KeepModel1']

class Command(BaseCommand):
    """
        clear all in app_name app except listed in keep_models list
    """
    help = 'clear all models except those listed in keep_models list'

    def handle(self, *args, **kwargs):
        my_app = apps.get_app_config('app_name')
        my_models = my_app.get_models()
        for model in my_models:
            if model.__name__ not in keep_models:
                model.objects.all().delete()

To run on the command line:

DJANGO_SETTINGS_MODULE=app_name.settings.local python manage.py clear_test_models
Answered By: Dawn Wages

You can do this using the Django shell:

>>> py manage.py shell
>>> from [your_app].models import [Your_Model] as md
>>> md.objects.all().delete()
>>> exit()

Remember to replace [your_app] with the app name and [Your_Model] with the name of the model you want to clear from the database.

Keep in mind this will remove the data in the table but not the table itself. If you wanted to do the latter you’d need to drop the table.

Answered By: Floella
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.