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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:52:27  来源:igfitidea点击:

What should I use instead of syncdb in Django 1.9?

pythondjangodjango-1.8

提问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?

我在谷歌上进行了快速搜索,但找不到答案 - 我应该使用什么来代替syncdb

采纳答案by tjati

syncdbis 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 syncdbyou should use makemigrationsand then migrate.

因此,syncdb您应该使用makemigrationsand 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 migratefor 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 makemigrationsand migratecommands that were introduced in django 1.7

您应该使用django 1.7 中引入的makemigrationsmigrate命令

https://docs.djangoproject.com/en/1.7/topics/migrations/

https://docs.djangoproject.com/en/1.7/topics/migrations/

回答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 makemigrationsto create migrations and migrateto migrate the database.

您绝对应该使用迁移系统。这使您可以跟踪 中的更改models.py,并为数据库创建迁移。迁移系统使用这些命令makemigrations来创建迁移和migrate迁移数据库。

If for whatever reason you need to create a database the same way syncdbdid it there is command flag that causes migrateto 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

syncdbhas some problem with db migration. so, after django 1.7 makemigrationsand migratehave been introduced. Now in django 1.9 syncdbhas been deprecated. try
1. python manage.py makemigrationswhich detects changes in db and creates one .pyfile as inside migrations folder 2. python manage.py migratewill apply the migrations to the database

syncdb数据库迁移有一些问题。所以,以后的Django 1.7makemigrationsmigrate相继出台。现在在 Django 1.9 中syncdb已被弃用。尝试
1.python manage.py makemigrations检测 db 中的更改并.py在迁移文件夹中创建一个文件 2.python manage.py migrate将迁移应用到数据库