Apply fix for migration testing

This commit is contained in:
Oliver Walters 2026-07-20 04:47:00 +00:00
parent 90c2245e5a
commit 8d9f2dce8c
1 changed files with 15 additions and 3 deletions

View File

@ -5,6 +5,7 @@ from typing import Any, Optional
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.db.migrations.state import StateApps
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
@ -117,11 +118,22 @@ class InvenTreeCustomStatusModelField(models.PositiveIntegerField):
return name, path, args, kwargs
def contribute_to_class(self, cls, name):
"""Add the _custom_key field to the model."""
"""Add the _custom_key field to the model.
Historical model states (rendered by the migration framework) already
declare the '{name}_custom_key' field explicitly, as it was captured as
an AddField operation when the migration was generated. Auto-adding it
here too would create a duplicate field on those historical models, so
skip it for anything other than the 'live' application registry.
"""
cls._meta.supports_custom_status = True
if not hasattr(self, '_custom_key_field') and not hasattr(
cls, f'{name}_custom_key'
is_historical = isinstance(cls._meta.apps, StateApps)
if (
not is_historical
and not hasattr(self, '_custom_key_field')
and not hasattr(cls, f'{name}_custom_key')
):
self.add_field(cls, name)