django


Convert data on AlterField django migration


I have a production database and need to keep safe the data. I want to change a Field in model and convert all data inside that database with this change.
Old field
class MyModel(models.Model):
field_name = models.TimeField()
Changed field
class MyModel(models.Model):
field_name = models.PositiveIntegerField()
Basically I want to convert the TimeField value (that has a Time object) in minutes.
Example: I have in an object time(hour=2, minute=0, second=0) and I want to convert that field value in all database table to 120 when I apply the migrate.
Safest way that I always do is:
create another field with field_name_new = models.PositiveIntegerField()
migrate this new field on production db
do data migration (convert field_name value and move the
converted value to field field_name_new)
change your code so that field_name_new is used
deploy the step 4
delete the field field_name
migrate the step 6 on production db and deploy it (the step 6)
there is only one bad side: you may loose some data in steps 4 and 5. but you can replicate these data to new field basically
In the past, I've always done this with the help of Djangos RunPython operation. Create a custom migration that handles the following.
Change name of field.
Add new field of desired type.
RunPython to handle the logic of converting from one to the other.
Delete old field.
def migrate_time_to_positive_int(apps, schema_editor):
MyModel = apps.get_model('myapp', 'MyModel')
for mm in MyModel.objects.all():
field_old_time = mm.field_name_old
field_new_int = field_old_time.total_seconds() / 60
mm.field_name = field_new_int
mm.save()
class Migration(migrations.Migration):
operations = [
migrations.RenameField(
model_name='mymodel',
old_name='field_name',
new_name='field_name_old',
),
migrations.AddField(
model_name='mymodel',
name='field_name',
field=models.PositiveIntegerField(),
),
migrations.RunPython(migrate_time_to_positive_int),
migrations.RemoveField(
model_name='mymodel',
name='field_name_old',
)
]
field_name_old.total_seconds() / 60 might need to be adjusted, but you get the idea.

Related Links

Django queryset. Convert output to list
Access missing value in form.cleaned_data
Configuring Django/Pinax Email
Django Book OUTDATE CSRF protection
Django mod_wsgi request takes 6 secs
Django query with random
how do I know I am dealing with the extra (empty) form within a formset?
del vs delete()
Django tests don't raise integrity error for CharField
Issues with admin panel
django complex query filter
Django user save hook
Django CSRF on 500 error page with a form
How to redirect to /home/, when matches (index.html)
Use database depending on subdomain
Django: converting Youtube URL to HTML embed code

Categories

HOME
sonarqube
adal
ng-idle
redux-form
pda
angular2-material
interface
pclxl
newrelic
big-o
python-multithreading
webix
accessibility
p2p
fosrestbundle
32bit-64bit
line
datastage
naivebayes
libc
endpoint
plink
cumulocity
altera
android-7.0-nougat
html5-appcache
gmm
sar
simpleitk
jquery-ias
ios10.2
fable-f#
android-sharedpreferences
flooding
angular-translate
soot
ceylon
sqsh
google-cast
sql-server-2000
kodi
jmockit
hard-drive
sammy.js
github3.py
biztalk-rule-engine
twos-complement
sql-delete
sim-toolkit
openproject
numberpicker
datacachefactory
oracle-service-bus
android-checkbox
opencmis
jide
fwrite
openresty
geotiff
podscms
magic-draw
azure-cli
java-bytecode-asm
colorbar
filehandle
arules
pdf-conversion
mach-o
java-websocket
d3-force-directed
objectscript
hadoop-2.7.2
wso2developerstudio
i386
gogs
riot
query-by-example
project-organization
yajsw
fuzzywuzzy
dynamics-nav-2016
batik
mod-proxy
amiga
typeconverter
phpldapadmin
dbaccess
deform
contrast
named-parameters
functional-java
maven-scm
notorm
solace-mq
model-driven-development
observium
time.h
facebook-timeline
android-loadermanager
uimanageddocument
catransform3d
mysql-error-1205
cuteeditor
facebook-iframe
fail-fast-fail-early
pbcopy
cardspace
idatareader

Resources

Encrypt Message