简体   繁体   English

如何在Django中将现有的CharField数据迁移到IntegerField

[英]How do I migrate existing CharField with data to IntegerField in Django

I have some models with CharFields in in my models.py. 我的models.py中有一些带有CharFields的模型。

eg 例如

class MyModel(models.Model):
    item_port = models.CharField(max_length=50)`

I have realised that this was a little stupid for a port and changed them all to IntegerFields as follows: 我已经意识到这对端口来说有点愚蠢,并将它们全部更改为IntegerFields,如下所示:

class MyModel(models.Model):
    item_port = models.IntegerField(null=True)

I am using sqlite3 in my local dev system and a MySQL type database for deployment. 我在我的本地开发系统和MySQL类型数据库中使用sqlite3进行部署。 Typically, I manufacture migrations for a new docker instance when deploying from scratch using: 通常,我使用以下方法从头开始部署时为新的docker实例制作迁移:

python manage.py makemigrations and python manage.py migrate python manage.py makemigrationspython manage.py migrate

However, I also have a record of all migrations made to my sqlite3 db in git for reference purposes. 但是,我还记录了在git中对我的sqlite3 db进行的所有迁移以供参考。

My main question is, can I get away with simply doing a makemigrations/migrate from CharField to IntegerField with existing data in the database (which on client sites will be a MySQL type database OR do I need to do something like this manually?: 我的主要问题是,我可以简单地使用数据库中的现有数据从CharField到IntegerField进行makemigrations / migrate(在客户端站点上将是MySQL类型数据库,还是我需要手动执行此类操作?)

https://docs.djangoproject.com/en/1.9/topics/migrations/#data-migrations https://docs.djangoproject.com/en/1.9/topics/migrations/#data-migrations

Thanks very much. 非常感谢。

If you notice that item_port should be a IntergerField not a CharField you can run 如果您发现item_port应该是一个IntergerField不是CharField可以运行

./manage.py schemamigration myapp --auto --update

Refering to the South documentation Advanced Commands and Data Migrations 请参阅南文档高级命令和数据迁移

Hope this will help :) 希望这会有所帮助:)

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

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