<filename>websites/migrations/0015_website_starter_config_field.py
# Generated by Django 3.1 on 2021-03-04 15:28

from django.db import migrations


COURSE_STARTER_SLUG = "course"


def add_file_widget(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starter = WebsiteStarter.objects.filter(slug=COURSE_STARTER_SLUG).first()
    collections = starter.config["collections"]
    if starter:
        for item in collections:
            if item["name"] == "resource":
                item["fields"].append(
                    {"label": "File", "name": "file", "widget": "file"}
                )
        starter.config = {"collections": collections}
        starter.save()


def remove_file_widget(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starter = WebsiteStarter.objects.filter(slug=COURSE_STARTER_SLUG).first()
    collections = starter.config["collections"]
    if starter:
        for item in collections:
            if item["name"] == "resource":
                for idx, field in enumerate(item["fields"]):
                    if field["name"] == "file":
                        item["fields"].pop(idx)
                        break
        starter.config = {"collections": collections}
        starter.save()


class Migration(migrations.Migration):
    dependencies = [
        ("websites", "0014_websitecontent_file"),
    ]

    operations = [
        migrations.RunPython(add_file_widget, remove_file_widget),
    ]

<filename>websites/migrations/0015_website_starter_config_field.py
# Generated by Django 3.1 on 2021-03-04 15:28

from django.db import migrations


COURSE_STARTER_SLUG = "course"


def add_file_widget(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starter = WebsiteStarter.objects.filter(slug=COURSE_STARTER_SLUG).first()
    collections = starter.config["collections"]
    if starter:
        for item in collections:
            if item["name"] == "resource":
                item["fields"].append(
                    {"label": "File", "name": "file", "widget": "file"}
                )
        starter.config = {"collections": collections}
        starter.save()


def remove_file_widget(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starter = WebsiteStarter.objects.filter(slug=COURSE_STARTER_SLUG).first()
    collections = starter.config["collections"]
    if starter:
        for item in collections:
            if item["name"] == "resource":
                for idx, field in enumerate(item["fields"]):
                    if field["name"] == "file":
                        item["fields"].pop(idx)
                        break
        starter.config = {"collections": collections}
        starter.save()


class Migration(migrations.Migration):
    dependencies = [
        ("websites", "0014_websitecontent_file"),
    ]

    operations = [
        migrations.RunPython(add_file_widget, remove_file_widget),
    ]

# Generated by Django 3.1 on 2021-03-04 15:28