有没有办法根据模型中的更改来更新数据库?
可能的重复:
更新 django 数据库以反映现有模型中的更改 < /p>
我过去使用过 Django,将其作为 ORM 工具时遇到的挫折之一是无法通过模型中的更改来更新现有数据库。 (Hibernate 在这方面做得非常好,并且使更新和大量修改模型并将其应用到现有数据库变得非常容易。)有没有一种方法可以在不擦除的情况下做到这一点每次都查数据库?每次更改我想要使用的模型后,都必须重新生成管理员用户和网站,这已经变得非常了。
Possible Duplicate:
update django database to reflect changes in existing models
I've used Django in the past and one of the frustrations I've had with it as an ORM tools is the inability to update an existing database with changes in the model. (Hibernate does this very well and makes things really easy for updating and heavily modifying a model and applying this to an existing database.) Is there a way to do this without wiping the database every time? It gets really old having to regenerate admin users and sites after every change in the model which I'd like to play with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你会想要看看南方。它提供了一个迁移系统,用于将架构更改以及数据从一个版本迁移到下一个版本。
它非常强大,并且绝大多数更改都可以通过简单地处理。
自动功能确实有其限制,特别是如果更改最终将在生产系统上运行,您应该检查代码
--auto< /code> 生成以确保它按照您的预期进行。
South 有一个很好的入门指南,并且有详细的记录。您可以在 http://south.aeracode.org 找到它
You will want to look into South. It provides a migrations system to migrate both schema changes as well as data from one version to the next.
It's quite powerful and the vast majority of changes can be handled simple by going
The auto functionality does have it limits, and especially if the change is going to be run on a production system eventually you should check the code
--auto
generated to be sure it's doing what you expect.South has a great guide to getting started and is well documented. You can find it at http://south.aeracode.org
否。
如
syncdb 的文档命令
指出:
No.
As the documentation of
syncdb
command states:South 似乎是大多数人解决此问题的方式,但真正快速且简单的方法是直接通过数据库的交互式 shell 更改数据库。只需启动 db shell(通常只是
dbshell
)并使用数据库语法手动更改、添加、删除需要更改的字段和表。您可能需要运行
manage.py sqlall appname
来查看Django在创建更新表时将运行的sql语句,然后使用这些语句根据需要更改数据库表和字段。Django 书中的“更改数据库架构”部分提供了一些如何执行此操作的示例: http://www.djangobook.com/en/1.0/chapter05/
South seems to be how most people solve this problem, but a really quick and easy way to do this is to change the db directly through your database's interactive shell. Just launch your db shell (usually just
dbshell
) and manually alter, add, drop the fields and tables you need changed using your db syntax.You may want to run
manage.py sqlall appname
to see the sql statements Django would run if it was creating the updated table, and then use those to alter the database tables and fields as required.The Making Changes to a Database Schema section of the Django book has a few examples of how to do this: http://www.djangobook.com/en/1.0/chapter05/
我手动进入数据库 - 无论什么可能适合您:MySQL、PostgreSQL 等 - 更改数据库信息,然后我相应地调整 models.py 以供参考。我知道有 Django South,但我不想费心使用另一个第三方应用程序。
I manually go into the database - whatever that may be for you: MySQL, PostgreSQL, etc. - to change database info, and then I adjust the models.py accordingly for reference. I know there is Django South, but I didn't want to bother with using another 3rd party application.