-
-
Notifications
You must be signed in to change notification settings - Fork 104
Open
Description
Description
When using MachineMixin with Django models, data migrations that use apps.get_model() fail because Django's historical models don't inherit class attributes like state_machine_name.
Error
ValueError: None is not a valid state machine name.
How to Reproduce
- Create a Django model with
MachineMixin:
from statemachine.mixins import MachineMixin
class MyModel(models.Model, MachineMixin):
state_machine_name = "myapp.MyStateMachine"
status = models.CharField(max_length=20)- Create a data migration using
RunPython:
def backfill_data(apps, schema_editor):
MyModel = apps.get_model("myapp", "MyModel")
for obj in MyModel.objects.all(): # Fails here
obj.some_field = "value"
obj.save()
class Migration(migrations.Migration):
operations = [
migrations.RunPython(backfill_data),
]- Run
python manage.py migrate
Suspected Cause
Django's apps.get_model() returns a dynamically created "historical" model class that doesn't include mixin class attributes. When MachineMixin.__init__ runs, self.state_machine_name is None, triggering the error.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels