如何在jango中使用south将CharField替换为ForeignKey? [英] How can I replace a CharField to ForeignKey using south in django?

查看:103
本文介绍了如何在jango中使用south将CharField替换为ForeignKey?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务模型:

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

我想将状态字段更改为ForeignKey,例如 state = models.ForeignKey(States)

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

国家/地区模型应为:

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

顺便说一句,我正在将 Django non-rel django mongodb一起使用。

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

使用Southern容易吗?还是其他方法?

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

推荐答案

我将分六个步骤(迁移)进行操作:

I will do it in six steps (migrations):


  • 第一步是data_migration(南命令):I创建状态对象。

  • 第二步执行schema_migration(南命令):我在Tasks中添加了一个fk,名称为state_bk

  • 第三步data_migration:迁移所有任务nd将状态分配给任务

  • 季度模式迁移:删除状态charfield并创建状态fk

  • 第五个data_migration:将值从state_bk迁移到state

  • 第六个schema_migration:删除state_bk字段

  • 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

这篇关于如何在jango中使用south将CharField替换为ForeignKey?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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