[Answer]-Inserting large amounts of data in Django via the shell

1👍

Use transaction, it will speed up your queries. Also replace the obj.save() call with the filter().update() combination, this is the fastest way to change the single field in DB:

from locations.models import City
from django.db import transaction
from django.template.defaultfilters import slugify
counter = 0
with transaction.atomic():
    for obj in City.objects.all():
        counter = counter + 1
        if counter % 1000 == 0:
            print counter
        slug = slugify(obj.city_name) + "-" + slugify(obj.region)
        City.objects.filter(pk=obj.pk).update(city_name_slug=slug)

Leave a comment