diff --git a/friendly_states/django.py b/friendly_states/django.py index b25ea1a..eb975e2 100644 --- a/friendly_states/django.py +++ b/friendly_states/django.py @@ -107,6 +107,11 @@ def deconstruct(self): return name, path, (self.machine,), kwargs def contribute_to_class(self, cls, name, *args, **kwargs): + if name == "_state": + raise ValueError( + "_state is an internal attribute used by Django " + "and should not be the name of a field." + ) super().contribute_to_class(cls, name, *args, **kwargs) self.machine.attr_name = self.attname diff --git a/tests/test_django.py b/tests/test_django.py index 0a5f0ad..6a45861 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -1,6 +1,6 @@ import pytest from django.core.exceptions import ValidationError -from django.db import IntegrityError +from django.db import IntegrityError, models from django.db.transaction import atomic from friendly_states.core import AttributeState @@ -172,3 +172,14 @@ class Machine(DjangoState): r"after declaring all states \(subclasses\)." ): StateField(Machine) + + +def test_underscore_state(): + with pytest.raises(ValueError): + class Model(models.Model): + class Meta: + app_label = "myapp" + + _state = StateField(TrafficLightMachine) + + str(Model)