Python 如何在 Django 中重置数据库?我收到命令“重置”未找到错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15454008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:10:07  来源:igfitidea点击:

How to reset db in Django? I get a command 'reset' not found error

pythondatabasedjangoreset

提问by Prem

Following this Django by Example tutotrial here: http://lightbird.net/dbe/todo_list.html

在此处通过示例教程遵循此 Django:http://lightbird.net/dbe/todo_list.html

The tutorial says:

教程说:

"This changes our table layout and we'll have to ask Django to reset and recreate tables:

manage.py reset todo; manage.py syncdb"

“这会改变我们的表格布局,我们将不得不要求 Django 重置和重新创建表格:

manage.py reset todo; manage.py syncdb

though, when I run manage.py reset todo, I get the error:

但是,当我运行时manage.py reset todo,出现错误:

$ python manage.py reset todo                                       
- Unknown command: 'reset'

Is this because I am using sqlite3 and not postgresql?

这是因为我使用的是 sqlite3 而不是 postgresql 吗?

Can somebody tell me what the command is to reset the database?

有人能告诉我重置数据库的命令是什么吗?

The command: python manage.py sqlclear todoreturns the error:

命令:python manage.py sqlclear todo返回错误:

$ python manage.py sqlclear todo    
CommandError: App with label todo could not be found.    
Are you sure your INSTALLED_APPS setting is correct?

So I added 'todo' to my INSTALLED_APPS in settings.py, and ran python manage.py sqlclear todoagain, resulting in this error:

所以我在settings.py中的INSTALLED_APPS中添加了'todo',然后python manage.py sqlclear todo再次运行,导致这个错误:

$ python manage.py sqlclear todo                                      
- NameError: name 'admin' is not defined

采纳答案by robertklep

resethas been replaced by flushwith Django 1.5, see:

reset已被flushDjango 1.5取代,见:

python manage.py help flush

回答by LisaD

It looks like the 'flush' answer will work for some, but not all cases. I needed not just to flush the values in the database, but to recreate the tables properly. I'm not using migrations yet (early days) so I really needed to drop all the tables.

看起来“刷新”答案适用于某些情况,但并非所有情况。我不仅需要刷新数据库中的值,还需要正确地重新创建表。我还没有使用迁移(早期)所以我真的需要删除所有表。

Two ways I've found to drop all tables, both require something other than core django.

我发现删除所有表的两种方法,都需要核心 django 以外的东西。

If you're on Heroku, drop all the tables with pg:reset:

如果您使用 Heroku,请使用 pg:reset 删除所有表:

heroku pg:reset DATABASE_URL
heroku run python manage.py syncdb

If you can install Django Extensions, it has a way to do a complete reset:

如果你可以安装 Django 扩展,它有一种方法可以完全重置:

python ./manage.py reset_db --router=default

回答by aendrew

Similar to LisaD's answer, Django Extensionshas a great reset_db command that totally drops everything, instead of just truncating the tables like "flush" does.

与 LisaD 的回答类似,Django Extensions有一个很棒的 reset_db 命令,可以完全删除所有内容,而不仅仅是像“flush”那样截断表。

python ./manage.py reset_db

python ./manage.py reset_db

Merely flushing the tables wasn't fixing a persistent error that occurred when I was deleting objects. Doing a reset_db fixed the problem.

仅仅刷新表并不能修复我删除对象时发生的持久性错误。做一个 reset_db 解决了这个问题。

回答by yask

For me this solved the problem.

对我来说,这解决了问题。

heroku pg:reset DATABASE_URL

heroku run bash
>> Inside heroku bash
cd app_name && rm -rf migrations && cd ..
./manage.py makemigrations app_name
./manage.py migrate

回答by Jan

Just a follow up to @LisaD'sanswer.
As of 2016 (Django 1.9), you need to type:

只是对@LisaD 的回答的跟进。
截至 2016 年 ( Django 1.9),您需要输入:

heroku pg:reset DATABASE_URL
heroku run python manage.py makemigrations
heroku run python manage.py migrate

This will give you a fresh new database within Heroku.

这将为您在 Heroku 中提供一个全新的数据库。

回答by Abhijeet Padhy

If you want to clean the whole database, you can use: python manage.py flushIf you want to clean database table of a Django app, you can use: python manage.py migrate appname zero

如果要清理整个数据库,可以使用: python manage.py flush如果要清理 Django 应用程序的数据库表,可以使用: python manage.py migrate appname zero

回答by Gursimran Singh

With django 1.11, simply delete all migration files from the migrationsfolder of each application (all files except __init__.py). Then

使用 django 1.11,只需从migrations每个应用程序的文件夹中删除所有迁移文件(除 之外的所有文件__init__.py)。然后

  1. Manually drop database.
  2. Manually create database.
  3. Run python3 manage.py makemigrations.
  4. Run python3 manage.py migrate.
  1. 手动删除数据库。
  2. 手动创建数据库。
  3. 运行python3 manage.py makemigrations
  4. 运行python3 manage.py migrate

And voilla, your database has been completely reset.

瞧,您的数据库已完全重置。

回答by Vaibhav Gupta

if you are using Django 2.0 Then

如果您使用的是 Django 2.0 那么

python manage.py flush 

will work

将工作

回答by bikram

  1. Just manually delete you database. Ensure you create backup first (in my case db.sqlite3is my database)

  2. Run this command manage.py migrate

  1. 只需手动删除您的数据库。确保首先创建备份(在我的情况下db.sqlite3是我的数据库)

  2. 运行这个命令 manage.py migrate

回答by Thusitha Deepal

python manage.py flush

deleted old db contents,

删除旧的数据库内容,

Don't forget to create new superuser:

不要忘记创建新的超级用户:

python manage.py createsuperuser