更改模型中的 Django 数据类型而不删除表
我创建了一个 Django 应用程序。现在我想更改模型中 1 个数据库字段的字段类型。由于数据库中已有一些具有当前类型的记录,我猜想通过简单地在 models.py 中更改它来更改类型是不可能的。通过谷歌搜索,我了解到可以通过 sql 控制台更改它,而无需删除表即可更改它。但作为 Django 新手,我无法做到这一点。有人可以帮我做到这一点吗?这是我的 models.py
class EmployeeDetails(models.Model):
userName = models.CharField(max_length=200)
designation = models.CharField(max_length=200)
employeeID = models.IntegerField()
contactNumber = models.CharField(max_length=200)
project = models.CharField(max_length=200)
dateOfJoin=models.TextField()
现在我想更改 dateOfJoin = models.DateField()。如何使用 sql 控制台命令将 TextField 更改为 DateField 而不删除表?
I have created a Django application. Now i wanted to change the field type for 1 of my db field in models. Since there are some records already in the database with the present type, i guess its not possible to change the type by simply changing it in models.py. By googling i came to know that it can be changed without dropping the tables by changing it through sql console. But being a Django newbie i am not able to do this. Can somebody help me to do this? This is my models.py
class EmployeeDetails(models.Model):
userName = models.CharField(max_length=200)
designation = models.CharField(max_length=200)
employeeID = models.IntegerField()
contactNumber = models.CharField(max_length=200)
project = models.CharField(max_length=200)
dateOfJoin=models.TextField()
Now i want to change the dateOfJoin = models.DateField(). How can i change the TextFieild to DateField without droping the table by using sql console commands?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的应用程序是South
以下是步骤(有关更多详细信息,请参阅文档):
这将创建一个迁移文件夹并伪造第一次迁移
然后在 models.py 中添加一个
new_dateOfJoin
DateField :这将立即创建并应用迁移(名为 0002_...)以在数据库中添加此字段
创建一个 datamigration 0003 文件,您只需将文本值转换为日期并将其保存在新字段中
然后应用此迁移:
修改您的 models.py 文件以注释掉旧的日期字段并再次迁移(这将创建 0004_.... 迁移文件)
最后一步是重命名 db 字段,这样您就不必修改您现有的代码:
这将创建一个名为
0005_rename_dateofjoin
的空架构迁移文件
手动修改此 0005 文件,如此处所述,但是使用
db.rename_column('app_foo', 'name', 'full_name')
就像 这个其他示例。删除整个表而不是这 5 个步骤可能会更容易,但如果您有应用程序的多个实例(例如工作的生产服务器和开发机器),您需要这种工具。
随着对南方有了更好的了解,人们也可以在不到 5 个步骤内完成此操作,但由于迁移有时很难撤消或重现,所以我更喜欢有不同的步骤......
The app you need is South
Here are the steps (refer to the doc for more details) :
this will create a migration folder and fake a first migration
Then add a
new_dateOfJoin
DateField in your models.py :this will create and apply immediatly a migration (named 0002_...) to add this field in the db
Create a datamigration 0003 file where you will just cast your text value to date and save it in your new field
Then apply this migration :
Modify your models.py file to comment out the old datefield and migrate again (this will create the 0004_.... migration file)
The last step is to rename the db field so you don't have to modify your existing code:
This will create an empty schema migration file named
0005_rename_dateofjoin
Manually modify this 0005 file like explained here , but use
db.rename_column('app_foo', 'name', 'full_name')
like in this other example.Dropping your whole table instead of these 5 steps can be easier, but if you have multiple instance of the app (like a working production server and a develpement machine) you need this kind of tools.
With better understanding of South, one could also do this in less than 5 steps, but as migrations can be tricky to undone or reproduce sometimes, I prefer to have distinct steps...
South 项目 通常用于迁移架构和数据。我不确定它是否可以用于将文本字段转换为日期字段,但如果可以的话我不会感到惊讶。
The South project is commonly used to migrate schemas and data. I'm not sure offhand if it can be used to convert a text field into a date field, but I wouldn't be surprised if it could.
我用另一种方式让它工作。我通过 sql 控制台选择了数据库,并使用 sql 查询添加了新列。这工作得很好:),非常感谢你们给予的帮助..
I got it working in another way. I selected my database through sql console and added new columns with sql query. And that worked just fine :), Thanks a lot for the help u guys gave..