Currently trying to migrate two fields of information into one field. We have many, many objects to go through and it takes roughly 12 minutes to loop through all the objects. That would mean the website would be down for 12 minutes and we do not want that. Is there any way to speed up this migration? It works, it's just slow.
from __future__ import unicode_literals
from django.db import migrations, models
def set_people(apps, schema_editor):
ParticipationCount = apps.get_model('partcount', 'ParticipationCount')
for row in ParticipationCount.objects.all():
row.people = row.male + row.female
row.save()
class Migration(migrations.Migration):
dependencies = [
('partcount', '0002_participationcount_people'),
]
operations = [
migrations.RunPython(set_people),
]
No is an acceptable answer in this case, I am just curious if there is a different way to do this, as loops can be slow. I am using Django 1.8 and python 2.
1 Answer 1
Unfortunately you're using django<2.2, since from 2.2 onwards you can use bulk_update , but you can still use something like
ParticipationCount.objects.all().update(
people=F("male") + F("female")
)
Which will do it in one query to save time. This will fail for certain fields in Postgres however as it requires some specific type casting, but this can be done within the query
-
1\$\begingroup\$ Works swimmingly! Thank you so much! Just had to import F from
django.db.models
to get it to work \$\endgroup\$Helana Brock– Helana Brock2020年02月26日 13:41:34 +00:00Commented Feb 26, 2020 at 13:41