How can I replace a CharField to ForeignKey using south in django?

喜夏-厌秋 提交于 2019-12-11 06:39:22

问题


My task model:

class Tasks(models.Model):
    state = models.CharField(max_length = 150)

I want to change the state field to ForeignKey like state = models.ForeignKey(States)

Where States model would be:

class States(model.Model):
    name = models.CharField(max_length = 150)

BTW I am using Django non-rel with django mongodb.

Is it easy to do using south?or is thr any other way to do it?


回答1:


I will do it in six steps (migrations):

  • First step a data_migration (south command): I create the States objects.
  • Second step a schema_migration (south command): I add a fk in Tasks the name will be state_bk
  • Third step a data_migration: Migrate all tasks and assigned the states to the tasks
  • Quarter schema_migration: Delete the states charfield and create states fk
  • Fifth data_migration: Migrate the values from state_bk to state
  • Sixth schema_migration: Delete state_bk field

This is easy, but a little tedious



来源:https://stackoverflow.com/questions/10462864/how-can-i-replace-a-charfield-to-foreignkey-using-south-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!