Removing custom field default method in Django

I implemented a field in my Django model with a default value that calls a custom class method I wrote. I ran and applied the migrations to my production deployment. Recently, I've found the field is no longer necessary, and I want to remove it and the custom class method from my model:

Model code:

from django.db import models

class OrganizationConfig(models.Model):
   ...
   def get_sla_default_value():  # type: ignore
        return {
            Priority.STAT: "1:00:00",
            Priority.ASAP: "2:00:00",
            Priority.TIMING_CRITICAL: "6:00:00",
            Priority.PREOPERATIVE: "24:00:00",
            Priority.ROUTINE: "48:00:00",
        }
   ...
   priority_slas = models.JSONField(default=get_sla_default_value)

Migration:

class Migration(migrations.Migration):

    dependencies = [
        ("core", "0085_alter_hangingprotocol_modality_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="OrganizationConfig",
            fields=[
                ...
                (
                    "priority_slas",
                    models.JSONField(
                        default=myapp.core.models.OrganizationConfig.get_sla_default_value
                    ),
                ),
                ...
            ],
        ),
    ]

However, the class method is referenced in the migration generated when I first implemented the logic (as shown above) and deleting the method results in an error when I try to apply my migrations:

Traceback (most recent call last):
  File "/app/./manage.py", line 31, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 117, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 58, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 229, in build_graph
    self.load_disk()
  File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 120, in load_disk
    migration_module = import_module(migration_path)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/app/myapp/core/migrations/0086_organizationconfig.py", line 8, in <module>
    class Migration(migrations.Migration):
  File "/app/myapp/core/migrations/0086_organizationconfig.py", line 33, in Migration
    default=myapp.core.models.OrganizationConfig.get_sla_default_value
AttributeError: type object 'OrganizationConfig' has no attribute 'get_sla_default_value'

I'm not sure how to edit the existing migration to remove the dependency on get_sla_default_value(). Simply deleting the section feels like it will cause issues in any existing deployments. Do I simply need to keep this dead method in my model forever? If it's useful, I am using Postgres 13 as my database.

Back to Top