Skip to content

Commit

Permalink
Disallow naming field _state in Django
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Sep 28, 2019
1 parent 74465e2 commit 2aa79e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions friendly_states/django.py
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion 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
Expand Down Expand Up @@ -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)

0 comments on commit 2aa79e3

Please sign in to comment.