[docs]classMachineMixin:"""This mixing allows a model to automatically instantiate and assign an ``StateMachine``. """state_field_name:str="state""""The model's state field name that will hold the state value."""state_machine_name:"str | None"=None"""A fully qualified name of the class, where it can be imported."""state_machine_attr:str="statemachine""""Name of the model's attribute that will hold the machine instance."""bind_events_as_methods:bool=False"""If ``True`` the state machine events triggers will be bound to the model as methods."""def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)ifnotself.state_machine_name:ifself._is_django_historical_model():returnraiseValueError(_("{!r} is not a valid state machine name.").format(self.state_machine_name))machine_cls=registry.get_machine_cls(self.state_machine_name)sm=machine_cls(self,state_field=self.state_field_name)setattr(self,self.state_machine_attr,sm,)ifself.bind_events_as_methods:sm.bind_events_to(self)@classmethoddef_is_django_historical_model(cls)->bool:"""Detect Django historical models created by ``apps.get_model()`` in migrations. Django sets ``__module__ = '__fake__'`` on these dynamically-created classes, which lack the user-defined class attributes like ``state_machine_name``. """returngetattr(cls,"__module__",None)=="__fake__"