<filename>creator/studies/migrations/0025_add_organizations.py
# Generated by Django 2.2.20 on 2021-04-29 01:30

from django.db import migrations, models
import django.db.models.deletion


def set_default_org(apps, schema_editor):
    """
    Create a default organization and put all studies and users under it.
    """
    Organization = apps.get_model("organizations", "Organization")
    User = apps.get_model("creator", "User")
    Study = apps.get_model("studies", "Study")
    org = Organization(
        id="300d175a-4904-44e5-878b-e8ead565a082", name="Default Organization"
    )
    org.save()

    for user in User.objects.all().iterator():
        user.organizations.add(org)
        user.save()

    for study in Study.objects.all().iterator():
        study.organization_id = org.pk
        study.save()


def delete_default_org(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("organizations", "0001_initial"),
        ("studies", "0024_add_additional_fields"),
    ]

    operations = [
        migrations.AddField(
            model_name="study",
            name="organization",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="organizations.Organization",
            ),
            preserve_default=False,
        ),
        migrations.RunPython(set_default_org, delete_default_org),
        migrations.AlterField(
            model_name="study",
            name="organization",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to="organizations.Organization",
            ),
            preserve_default=False,
        ),
    ]

<filename>creator/studies/migrations/0025_add_organizations.py
# Generated by Django 2.2.20 on 2021-04-29 01:30

from django.db import migrations, models
import django.db.models.deletion


def set_default_org(apps, schema_editor):
    """
    Create a default organization and put all studies and users under it.
    """
    Organization = apps.get_model("organizations", "Organization")
    User = apps.get_model("creator", "User")
    Study = apps.get_model("studies", "Study")
    org = Organization(
        id="300d175a-4904-44e5-878b-e8ead565a082", name="Default Organization"
    )
    org.save()

    for user in User.objects.all().iterator():
        user.organizations.add(org)
        user.save()

    for study in Study.objects.all().iterator():
        study.organization_id = org.pk
        study.save()


def delete_default_org(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("organizations", "0001_initial"),
        ("studies", "0024_add_additional_fields"),
    ]

    operations = [
        migrations.AddField(
            model_name="study",
            name="organization",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="organizations.Organization",
            ),
            preserve_default=False,
        ),
        migrations.RunPython(set_default_org, delete_default_org),
        migrations.AlterField(
            model_name="study",
            name="organization",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to="organizations.Organization",
            ),
            preserve_default=False,
        ),
    ]

# Generated by Django 2.2.20 on 2021-04-29 01:30 Create a default organization and put all studies and users under it.