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

class Migration(migrations.Migration):

    dependencies = [
        ('partcount', '0002_participationcount_people'),

    operations = [

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

    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\$ Feb 26, 2020 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.