我有一个要重命名但也要更改其数据的模型。例如,模型字段foo
包含char数据1
... 6
,我想通过以下方式映射此数据:
1
,2
-> 1
&& 3
,4
-> 2
&& 5
-> 3
&& 6
->4
我convert_to_south
在我的应用程序上进行了操作,该应用程序创建了0001_initial
然后更改了新的新字段,models.py
并完成schemamigration
了0002_initial.py
。现在如何修改现有数据?这是我的迁移:
0001_initial
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Applicant'
db.create_table(u'registrar_applicant', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('registered_at', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
('workshop', self.gf('django.db.models.fields.CharField')(max_length=20)),
('name', self.gf('django.db.models.fields.CharField')(max_length=255)),
('semester', self.gf('django.db.models.fields.CharField')(max_length=1)),
('python_experience', self.gf('django.db.models.fields.CharField')(max_length=1)),
('phone_number', self.gf('django.db.models.fields.CharField')(max_length=10, blank=True)),
('email', self.gf('django.db.models.fields.EmailField')(max_length=75)),
('solved_puzzle', self.gf('django.db.models.fields.BooleanField')(default=False)),
))
db.send_create_signal(u'registrar', ['Applicant'])
def backwards(self, orm):
# Deleting model 'Applicant'
db.delete_table(u'registrar_applicant')
models = {
u'registrar.applicant': {
'Meta': {'ordering': "['-solved_puzzle', 'registered_at', 'semester', 'name']", 'object_name': 'Applicant'},
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'phone_number': ('django.db.models.fields.CharField', [], {'max_length': '10', 'blank': 'True'}),
'python_experience': ('django.db.models.fields.CharField', [], {'max_length': '1'}),
'registered_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'semester': ('django.db.models.fields.CharField', [], {'max_length': '1'}),
'solved_puzzle': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'workshop': ('django.db.models.fields.CharField', [], {'max_length': '20'})
}
}
complete_apps = ['registrar']
0002_initial
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Applicant'
db.create_table(u'registrar_applicant', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('registered_at', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
('workshop', self.gf('django.db.models.fields.CharField')(max_length=20)),
('name', self.gf('django.db.models.fields.CharField')(max_length=255)),
('year', self.gf('django.db.models.fields.CharField')(max_length=1)),
('python_experience', self.gf('django.db.models.fields.CharField')(max_length=1)),
('phone_number', self.gf('django.db.models.fields.CharField')(max_length=10, blank=True)),
('email', self.gf('django.db.models.fields.EmailField')(max_length=75)),
('solved_puzzle', self.gf('django.db.models.fields.BooleanField')(default=False)),
))
db.send_create_signal(u'registrar', ['Applicant'])
def backwards(self, orm):
# Deleting model 'Applicant'
db.delete_table(u'registrar_applicant')
models = {
u'registrar.applicant': {
'Meta': {'ordering': "['-solved_puzzle', 'registered_at', 'year', 'name']", 'object_name': 'Applicant'},
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'phone_number': ('django.db.models.fields.CharField', [], {'max_length': '10', 'blank': 'True'}),
'python_experience': ('django.db.models.fields.CharField', [], {'max_length': '1'}),
'registered_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'solved_puzzle': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'workshop': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
'year': ('django.db.models.fields.CharField', [], {'max_length': '1'})
}
}
complete_apps = ['registrar']
为此,您必须创建一个数据迁移
# Run this command from the shell
python manage.py datamigration <app_name> change_foo_values
这将创建一个名为的迁移文件0003_change_foo_values.py
。您将必须手动编辑文件。
# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
# Note: Don't use "from appname.models import ModelName".
# Use orm.ModelName to refer to models in this application,
# and orm['appname.ModelName'] for models in other applications.
orm.Applicant.objects.filter(foo__in=['1', '2']).update(foo='1')
orm.Applicant.objects.filter(foo__in=['3', '4']).update(foo='2')
orm.Applicant.objects.filter(foo__in=['5', '6']).update(foo='3')
def backwards(self, orm):
"Write your backwards methods here."
# This migration cannot be reversed
pass # don't do anything when running reverse migration
# or
raise RuntimeError("Cannot reverse this migration") # stop south from reverting beyond this migration
注意:您将无法还原到该列的先前值。如果这不是问题,那就足够了。
但是,如果您确实希望能够撤消迁移,则建议创建一个新字段,该字段将保留每一行的旧值。
PS:您的迁移文件似乎不对。initial
一个应用程序不应有两次迁移。convert_to_south
重新命名模型(似乎尚未重命名)后,您是否再次运行?convert_to_south
最初将现有模型转换为南方时,只需运行一次。之后,您需要创建架构迁移。最简单的方法是schemamigration
使用appname并--auto
作为参数运行管理命令。
python manage.py schemamigration <app_name> --auto
PPS:如果要重命名模型,请创建一个空迁移并自己编写迁移代码,只需使用即可db.rename_table
。如果重命名模型后尝试创建自动模式迁移,则IIRC south将在创建新表之前删除旧表。
python manage.py schemamigration <app_name> --empty rename_model_x_to_y
并在您的迁移文件中
# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
"Write your forwards methods here."
# Note: Don't use "from appname.models import ModelName".
# Use orm.ModelName to refer to models in this application,
# and orm['appname.ModelName'] for models in other applications.
db.rename_table('old', 'new')
def backwards(self, orm):
"Write your backwards methods here."
db.rename_table('new', 'old')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句