
from django.db import migrations


def get_birth_year(apps, schema_editor):
    Person = apps.get_model("people", "Person")
    for person in Person.objects.all():
        birth_year = person.birth_date.split("-")[0]
        person.birth_date = birth_year
        person.save()


class Migration(migrations.Migration):

    dependencies = [("people", "0033_auto_20210928_1007")]

    operations = [
        migrations.RunPython(get_birth_year, migrations.RunPython.noop)
    ]


from django.db import migrations


def get_birth_year(apps, schema_editor):
    Person = apps.get_model("people", "Person")
    for person in Person.objects.exclude(birth_date="").iterator():
        birth_year = person.birth_date.split("-")[0]
        person.birth_date = birth_year
        person.save()


class Migration(migrations.Migration):

    dependencies = [("people", "0033_auto_20210928_1007")]

    operations = [
        migrations.RunPython(get_birth_year, migrations.RunPython.noop)
    ]

Improve performance of birth date data migration
Improve performance of birth date data migration

  
  from django.db import migrations
  
  
  def get_birth_year(apps, schema_editor):
      Person = apps.get_model("people", "Person")
-     for person in Person.objects.all():
+     for person in Person.objects.exclude(birth_date="").iterator():
          birth_year = person.birth_date.split("-")[0]
          person.birth_date = birth_year
          person.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [("people", "0033_auto_20210928_1007")]
  
      operations = [
          migrations.RunPython(get_birth_year, migrations.RunPython.noop)
      ]
  
Improve performance of birth date data migration
## Code Before:

from django.db import migrations


def get_birth_year(apps, schema_editor):
    Person = apps.get_model("people", "Person")
    for person in Person.objects.all():
        birth_year = person.birth_date.split("-")[0]
        person.birth_date = birth_year
        person.save()


class Migration(migrations.Migration):

    dependencies = [("people", "0033_auto_20210928_1007")]

    operations = [
        migrations.RunPython(get_birth_year, migrations.RunPython.noop)
    ]

## Instruction:
Improve performance of birth date data migration
## Code After:

from django.db import migrations


def get_birth_year(apps, schema_editor):
    Person = apps.get_model("people", "Person")
    for person in Person.objects.exclude(birth_date="").iterator():
        birth_year = person.birth_date.split("-")[0]
        person.birth_date = birth_year
        person.save()


class Migration(migrations.Migration):

    dependencies = [("people", "0033_auto_20210928_1007")]

    operations = [
        migrations.RunPython(get_birth_year, migrations.RunPython.noop)
    ]
