@@ -15,66 +15,66 @@ This way, the database can be migrated without downtime, and the field can be po
15
15
Don't forget to make the field non-nullable in a separate migration after the data has been populated.
16
16
You can achieve this by following these steps:
17
17
18
- - #. Set the new field as ``null=True `` and ``blank=True `` in the model.
18
+ #. Set the new field as ``null=True `` and ``blank=True `` in the model.
19
19
20
- .. code-block :: python
20
+ .. code-block :: python
21
21
22
- class MyModel (models .Model ):
23
- new_field = models.CharField(
24
- max_length = 100 , null = True , blank = True , default = " default"
25
- )
22
+ class MyModel (models .Model ):
23
+ new_field = models.CharField(
24
+ max_length = 100 , null = True , blank = True , default = " default"
25
+ )
26
26
27
- - #. Make sure that the field is always populated with a proper value in the new code,
28
- and the code handles the case where the field is null.
27
+ #. Make sure that the field is always populated with a proper value in the new code,
28
+ and the code handles the case where the field is null.
29
29
30
- .. code-block :: python
30
+ .. code-block :: python
31
31
32
- if my_model.new_field in [None , " default" ]:
33
- pass
32
+ if my_model.new_field in [None , " default" ]:
33
+ pass
34
34
35
35
36
- # If it's a boolean field, make sure that the null option is removed from the form.
37
- class MyModelForm (forms .ModelForm ):
38
- def __init__ (self , * args , ** kwargs ):
39
- super ().__init__ (* args, ** kwargs)
40
- self .fields[" new_field" ].widget = forms.CheckboxInput()
41
- self .fields[" new_field" ].empty_value = False
36
+ # If it's a boolean field, make sure that the null option is removed from the form.
37
+ class MyModelForm (forms .ModelForm ):
38
+ def __init__ (self , * args , ** kwargs ):
39
+ super ().__init__ (* args, ** kwargs)
40
+ self .fields[" new_field" ].widget = forms.CheckboxInput()
41
+ self .fields[" new_field" ].empty_value = False
42
42
43
- - #. Create the migration file (let's call this migration ``app 0001 ``),
44
- and mark it as ``Safe.before_deploy ``.
43
+ #. Create the migration file (let's call this migration ``app 0001 ``),
44
+ and mark it as ``Safe.before_deploy ``.
45
45
46
- .. code-block :: python
46
+ .. code-block :: python
47
47
48
- from django.db import migrations, models
49
- from django_safemigrate import Safe
48
+ from django.db import migrations, models
49
+ from django_safemigrate import Safe
50
50
51
51
52
- class Migration (migrations .Migration ):
53
- safe = Safe.before_deploy
52
+ class Migration (migrations .Migration ):
53
+ safe = Safe.before_deploy
54
54
55
- - #. Create a data migration to populate all null values of the new field with a proper value (let's call this migration ``app 0002 ``),
56
- and mark it as ``Safe.after_deploy ``.
55
+ #. Create a data migration to populate all null values of the new field with a proper value (let's call this migration ``app 0002 ``),
56
+ and mark it as ``Safe.after_deploy ``.
57
57
58
- .. code-block :: python
58
+ .. code-block :: python
59
59
60
- from django.db import migrations
60
+ from django.db import migrations
61
61
62
62
63
- def migrate (apps , schema_editor ):
64
- MyModel = apps.get_model(" app" , " MyModel" )
65
- MyModel.objects.filter(new_field = None ).update(new_field = " default" )
63
+ def migrate (apps , schema_editor ):
64
+ MyModel = apps.get_model(" app" , " MyModel" )
65
+ MyModel.objects.filter(new_field = None ).update(new_field = " default" )
66
66
67
67
68
- class Migration (migrations .Migration ):
69
- safe = Safe.after_deploy
68
+ class Migration (migrations .Migration ):
69
+ safe = Safe.after_deploy
70
70
71
- operations = [
72
- migrations.RunPython(migrate),
73
- ]
71
+ operations = [
72
+ migrations.RunPython(migrate),
73
+ ]
74
74
75
- - #. After the deploy has been completed, create a new migration to set the field as non-nullable (let's call this migration ``app 0003 ``).
76
- Run this migration on a new deploy, you can mark it as ``Safe.before_deploy `` or ``Safe.always ``.
77
- - #. Remove any handling of the null case from the code.
75
+ #. After the deploy has been completed, create a new migration to set the field as non-nullable (let's call this migration ``app 0003 ``).
76
+ Run this migration on a new deploy, you can mark it as ``Safe.before_deploy `` or ``Safe.always ``.
77
+ #. Remove any handling of the null case from the code.
78
78
79
79
At the end, the deploy should look like this:
80
80
@@ -93,37 +93,37 @@ all usages of the field should be removed from the code before the field is remo
93
93
and the field should be nullable. **
94
94
You can achieve this by following these steps:
95
95
96
- - #. Remove all usages of the field from the code.
97
- - #. Set the field as ``null=True `` and ``blank=True `` in the model.
96
+ #. Remove all usages of the field from the code.
97
+ #. Set the field as ``null=True `` and ``blank=True `` in the model.
98
98
99
- .. code-block :: python
99
+ .. code-block :: python
100
100
101
- class MyModel (models .Model ):
102
- field_to_delete = models.CharField(max_length = 100 , null = True , blank = True )
101
+ class MyModel (models .Model ):
102
+ field_to_delete = models.CharField(max_length = 100 , null = True , blank = True )
103
103
104
- - #. Create the migration file (let's call this migration ``app 0001 ``),
105
- and mark it as ``Safe.before_deploy ``.
104
+ #. Create the migration file (let's call this migration ``app 0001 ``),
105
+ and mark it as ``Safe.before_deploy ``.
106
106
107
- .. code-block :: python
107
+ .. code-block :: python
108
108
109
- from django.db import migrations, models
110
- from django_safemigrate import Safe
109
+ from django.db import migrations, models
110
+ from django_safemigrate import Safe
111
111
112
112
113
- class Migration (migrations .Migration ):
114
- safe = Safe.before_deploy
113
+ class Migration (migrations .Migration ):
114
+ safe = Safe.before_deploy
115
115
116
- - #. Create a migration to remove the field from the database (let's call this migration ``app 0002 ``),
117
- and mark it as ``Safe.after_deploy ``.
116
+ #. Create a migration to remove the field from the database (let's call this migration ``app 0002 ``),
117
+ and mark it as ``Safe.after_deploy ``.
118
118
119
- .. code-block :: python
119
+ .. code-block :: python
120
120
121
- from django.db import migrations, models
122
- from django_safemigrate import Safe
121
+ from django.db import migrations, models
122
+ from django_safemigrate import Safe
123
123
124
124
125
- class Migration (migrations .Migration ):
126
- safe = Safe.after_deploy
125
+ class Migration (migrations .Migration ):
126
+ safe = Safe.after_deploy
127
127
128
128
At the end, the deploy should look like this:
129
129
0 commit comments