如何在Django应用程序中删除数据库中的所有数据?在以前的版本中manage.py reset APPNAME
完成了这项工作,但已被弃用。
如果我们想要使用命令行删除应用程序中的所有数据,我们现在应该做什么?
答案 0 :(得分:15)
reset
和sqlreset
都只是围绕其他管理命令的包装器。只需运行以下内容即可复制sqlreset
:
python manage.py sqlclear myapp
python manage.py sqlall myapp
reset
仅用于在数据库上自动运行sqlreset
的结果。就个人而言,我认为删除它是一个奇妙的想法。但是,如果你想要类似的功能,你可以将输出管道传输到你的数据库的shell命令。
对于PostgreSQL,例如:
python manage.py sqlclear myapp | psql mydatabase
python manage.py sqlall myapp | psql mydatabase
答案 1 :(得分:8)
如果你想要一个适用于大多数数据库类型的单一命令,你可以将sqlclear生成的drop table语句传递给dbshell
python manage.py sqlclear myapp | python manage.py dbshell
答案 2 :(得分:5)
from django.contrib.contenttypes.models import ContentType
for ct in ContentType.objects.all()
ct.model_class().objects.all().delete()
答案 3 :(得分:2)
现在Django默认情况下集成了迁移,您首先需要先为应用程序进行迁移,然后再删除。
这是至少与 Django 1.8 一起工作的命令行(由要删除所有相关数据的应用程序替换,并且:
# 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>
答案 4 :(得分:0)
如果要从命令行执行此操作,请创建以下custom 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()))
将其复制到任何应用文件夹中的app/management/commands
文件夹,然后使用
./manage.py app_db_tables_reset any_installed_app_name
该命令在django_commands
package中可用,您可以使用
pip install git+http://github.com/mrts/django-commands.git
并将其添加到INSTALLED_APPS
以激活命令。
使用Django 1.9测试,它可能适用于1.8,也可能不适用。