简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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