Python 在 Django 1.9 中我应该使用什么来代替 syncdb?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29683494/
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
What should I use instead of syncdb in Django 1.9?
提问by d33tah
Take a look at this:
看看这个:
$ pypy ./manage.py syncdb
/usr/lib64/pypy-2.4.0/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
(cut)
I ran a quick google search, but could not find the answer - what should I be using instead of syncdb
?
采纳答案by tjati
syncdb
is deprecated because of the migration system, introduced with django 1.7.
syncdb
由于django 1.7引入的迁移系统而被弃用。
Now you can trackyour changes using makemigrations
. This transforms your model changes into python code to make them deployable to another databases. When you have further modifications you need applied to the database, you can use data migrations.
现在,您可以跟踪使用更改makemigrations
。这会将您的模型更改转换为 Python 代码,使其可部署到其他数据库。当您需要对数据库进行进一步修改时,您可以使用数据迁移。
After you created the migrations you have to applythem: migrate
.
创建迁移后,您必须应用它们:migrate
.
So instead of using syncdb
you should use makemigrations
and then migrate
.
因此,syncdb
您应该使用makemigrations
and then代替使用migrate
。
Workflow on development after you changed something in your models:
更改模型中的某些内容后的开发工作流程:
./manage.py makemigrations
./manage.py migrate
And on your production system:
在您的生产系统上:
./manage.py migrate
Bonus: you do not need to run migrate
for each change. If you have multiple changes not applied yet django will run them in the correct order for you.
奖励:您不需要migrate
为每次更改都运行。如果您有多个更改尚未应用,django 将为您按正确的顺序运行它们。
回答by dizballanze
You should use the makemigrations
and migrate
commands that were introduced in django 1.7
您应该使用django 1.7 中引入的makemigrations
和migrate
命令
回答by Don Mums
You should definitely use migration system. Which lets you track changes in your models.py
, and create migrations for the database. The migration system uses the commands makemigrations
to create migrations and migrate
to migrate the database.
您绝对应该使用迁移系统。这使您可以跟踪 中的更改models.py
,并为数据库创建迁移。迁移系统使用这些命令makemigrations
来创建迁移和migrate
迁移数据库。
If for whatever reason you need to create a database the same way syncdb
did it there is command flag that causes migrate
to work the same way. You should only do this if you REALLYneed it and you know what you are doing. For example to create an empty database on for a continuous integration system of your choice.
如果出于某种原因您需要以相同的方式创建数据库syncdb
,那么命令标志会导致migrate
以相同的方式工作。只有当你真的需要它并且你知道你在做什么时,你才应该这样做。例如,为您选择的持续集成系统创建一个空数据库。
python manage.py migrate auth
# performs migrations for auth and contenttypes contrib apps
python manage.py migrate --run-syncdb
# creates the rest of the database
Tested on Django 1.9.1.
在 Django 1.9.1 上测试。
回答by Amrendra
syncdb
has some problem with db migration. so, after django 1.7 makemigrations
and migrate
have been introduced.
Now in django 1.9 syncdb
has been deprecated.
try
1. python manage.py makemigrations
which detects changes in db and creates one .py
file as inside migrations folder
2. python manage.py migrate
will apply the migrations to the database
syncdb
数据库迁移有一些问题。所以,以后的Django 1.7makemigrations
和migrate
相继出台。现在在 Django 1.9 中syncdb
已被弃用。尝试
1.python manage.py makemigrations
检测 db 中的更改并.py
在迁移文件夹中创建一个文件 2.python manage.py migrate
将迁移应用到数据库