How to delete all data for one application in Django 1.4 now that reset has disappeared?

How to delete all data in a database for a Django application? In a previous version of manage.py reset APPNAME , this work was done, but it is deprecated.

What do we need to do if we want to delete all data from the application using the command line?

+6
source share
5 answers

reset and sqlreset were like wrappers around other control commands. sqlreset in particular can be duplicated by a simple run:

 python manage.py sqlclear myapp python manage.py sqlall myapp 

reset is used to automatically run the sqlreset result in the database. Personally, I think this is a fantastic idea. However, if you need similar functionality, you can simply pass the output to your database shell commands.

For PostgreSQL, for example:

 python manage.py sqlclear myapp | psql mydatabase python manage.py sqlall myapp | psql mydatabase 
+15
source

If you need a single command that should work with most types of databases, you can include the table table statements that sqlclear generates in dbshell

python manage.py sqlclear myapp | python manage.py dbshell

+8
source
 from django.contrib.contenttypes.models import ContentType for ct in ContentType.objects.all() ct.model_class().objects.all().delete() 
+5
source

Now that Django integrates the default migrations, first you need to do the migration for your application, first do not use, and then delete.

Here is the command line that works with at least Django 1.8 (replacing the application you want to delete all related data, and:

 # First, update the DB so it thinks no migrations were applied to the app python manage.py migrate --fake <app_name> zero # Erase all migrations in the app folder rm -r "<app_name>/migrations/*" # Erase the application tables python manage.py sqlclear <app_name> | python manage.py dbshell # Recreate the app tables, that will be empty python manage.py makemigrations <app_name> python manage.py migrate <app_name> 
+2
source

DIY

If you want to do this from the command line, create the following user command :

 from django.core.management.base import AppCommand, CommandError from django.utils.six.moves import input from django.db import DEFAULT_DB_ALIAS, connections class Command(AppCommand): help = ( 'Removes ALL DATA related to the given app from the database ' 'by calling model.objects.all().delete() for all app models. ' 'This also removes related data in other apps via cascade.' ) def add_arguments(self, parser): super(Command, self).add_arguments(parser) parser.add_argument( '--noinput', '--no-input', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.', ) parser.add_argument( '--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a database to reset. Defaults to the "default" database.', ) def handle_app_config(self, app_config, **options): app_label = app_config.label database = options['database'] interactive = options['interactive'] db_name = connections[database].settings_dict['NAME'] confirm = (ask_confirmation(app_label, db_name) if interactive else 'yes') if confirm == 'yes': for model in app_config.get_models(): model.objects.using(database).all().delete() self.stdout.write('Reset done.\n') else: self.stdout.write("Reset cancelled.\n") def ask_confirmation(app_label, db_name): return input("""You have requested a reset of the application {app_label}. This will IRREVERSIBLY DESTROY all data related to the app currently in the {db_name} database, and return each table to empty state. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """.format(**locals())) 

Copy it to the app/management/commands folder in any of your application folders and run it using

 ./manage.py app_db_tables_reset any_installed_app_name 

Finished package

The command is available in the django_commands package, you can install it using

 pip install git+http://github.com/mrts/django-commands.git 

and add it to INSTALLED_APPS to activate the command.

Tested with Django 1.9, it may or may not work with 1.8.

0
source

Source: https://habr.com/ru/post/919348/


All Articles