在Django模型中添加新列 [英] Adding new columns in Django models

查看:249
本文介绍了在Django模型中添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个示例Django应用程序,其中包含多个模型和填充数据。



现在,我需要向其中一个模型添加一个新列? / p>

这是我的疑虑吗?


  1. 如果在添加后进行syncdb,会发生什么

  2. 还是删除所有列后会创建一个新的表?

有没有更好的方法来解决这个问题?

解决方案

syncdb不会添加新列,如果表已经存在,则不会创建新表。我以前做的事情只是在模型中添加字段名称之后。在shell中输入:

  $ python manage.py dbshel​​l 

您将直接在你的数据库shell(mysql或者psql)到你使用的是什么数据库


mysql> | PSQL> ALTER TABLE< table_name> ADD列varchar(100);

,它会将新列添加到表中,如果表已经填充了,则不重要。


I have created a sample Django application with multiple models within it and populated data.

Now, I need to add a new column to one of the models?

Here are my concerns?

  1. What will happen if I do syncdb after adding a column to the model , will it just alter the table and add the new column?
  2. Or will it create a new table after deleting all the columns?

Is there any better way to tackle this issue?

解决方案

syncdb will not add new column and if the table already exist it will not create no new table. the thing i used to do is simply after adding the field name in your model. get within the shell and type:

$ python manage.py dbshell 

you will get directly within your database shell (mysql or psql) it up to what database
you are using.

mysql> | psql> ALTER TABLE <table_name> ADD column varchar(100);

and it will add the new column to your table, doesn't matter if the table it already populated or not.

这篇关于在Django模型中添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆