简体   繁体   English

如何在Django模型中添加字段?

[英]How to add a field in Django model?

在Django中,如果我有一个包含4个字段的模型,但现在我想添加一个新字段,如何将数据保存在旧模型中?

You'll need to perform a "migration", and the current best way to do this in Django is with a tool called South . 您需要执行“迁移”,目前在Django中执行此操作的最佳方法是使用名为South的工具。

Check out their tutorial . 看看他们的教程 Make sure you make a copy of your db before you do this, since you may mess something up if it's your first time using South. 确保在执行此操作之前复制数据库,因为如果您是第一次使用South,可能会弄乱一些内容。

use South 用南

pip install South

firstly, keep your django models match your current database tables exactly, then run: 首先,让你的django模型与你当前的数据库表完全匹配,然后运行:

python manage.py schemamigration myapp --initial
python manage.py migrate myapp --fake 
#Make changes to your django model (add new field)
python manage.py schemamigration myapp --auto 
python manage.py migrate myapp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM