Alors voici comment j'ai procédé:
Migration N°1
J'ajoute le nouveau champ
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Migration(migrations.Migration):
dependencies = [
('businessCanvas', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='businesscanvaselement',
name='newtype',
field=models.CharField(max_length=20, null=True, choices=[(1, b'KeyPartner'), (2, b'KeyActivitie'), (3, b'ValueProposition'), (4, b'CustomerRelationship'), (5, b'KeyResource'), (6, b'Channel'), (7, b'CustomerSegment'), (8, b'CostStructure'), (9, b'RevenueStream'), (10, b'BrainstormingSpace')]),
),
]
|
Migration N°2
Populate mon nouveau champ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Migration(migrations.Migration):
def gen_newtype(apps, schema_editor):
MyModel = apps.get_model('businessCanvas', 'BusinessCanvasElement')
MySecondModel = apps.get_model('businessCanvas', 'BusinessCanvasType')
for row in MyModel.objects.all():
id = row.type_id
for row2 in MySecondModel.objects.all():
if(row2.id == id):
row.newtype = row2.name
row.save()
dependencies = [
('businessCanvas', '0002_businesscanvaselement_newtype'),
]
operations = [
migrations.RunPython(gen_newtype, reverse_code=migrations.RunPython.noop),
]
|
Migration N°3
Suppression du null=True
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Migration(migrations.Migration):
dependencies = [
('businessCanvas', '0003_auto_20150604_1253'),
]
operations = [
migrations.AlterField(
model_name='businesscanvaselement',
name='newtype',
field=models.CharField(max_length=20, choices=[(1, b'KeyPartner'), (2, b'KeyActivitie'), (3, b'ValueProposition'), (4, b'CustomerRelationship'), (5, b'KeyResource'), (6, b'Channel'), (7, b'CustomerSegment'), (8, b'CostStructure'), (9, b'RevenueStream'), (10, b'BrainstormingSpace')]),
),
]
|
Migration N°4
Suppression de l'ancien champ
1
2
3
4
5
6
7
8
9
10
11
12 | class Migration(migrations.Migration):
dependencies = [
('businessCanvas', '0004_auto_20150604_1257'),
]
operations = [
migrations.RemoveField(
model_name='businesscanvaselement',
name='type',
),
]
|
Migration N°5
Rename le nouveau champ
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Migration(migrations.Migration):
dependencies = [
('businessCanvas', '0005_auto_20150604_1304'),
]
operations = [
migrations.RenameField(
model_name='businesscanvaselement',
old_name='newtype',
new_name='type',
),
]
|
Ancienne DB:
Textes complets | id | title | comment | date | disactivated | company_id | type
Nouvelle DB:
Textes complets | id | title | comment | date | disactivated | company_id | type
A la différence prêt que type contient le nom et non plus l'ID de ma FOREIGNKEY.
Y aurait-il moyen de faire comprendre a Django que je veut qu'il stocke l'integer mais tout en gardant la possibilité de retrouver le name associé?
Mieux encore:
Y aurait-il moyen de pouvair faire appel a ce name associé tout comme je le faisait avec ma table de DB? de manière a ne pas avoir a refactorer toutes mes pages.