Commits

Ian George committed f77d006

Bug on field

  • Participants
  • Parent commits b545192

Comments (0)

Files changed (1)

statemachine/fields.py

         defaults.update(kwargs)
         super(FSM_StateField, self).__init__(self, **defaults)
 
+    def setup(self, state_name):
+        if not self._machine:
+            self._machine = deepcopy(self.__base_machine)
+
+        self._machine.set_initial_state(state_name)
+
     def db_type(self, connection):
         return "char(50)"
 
             name = value.state
         except AttributeError:
             name = value
-        self._machine = deepcopy(self.__base_machine)
-        self._machine.set_initial_state(name)
+
+        self.setup(name)
+
         return self._machine
 
+    def get_prep_value(self, value):
+        return value
+
     def get_db_prep_value(self, value, connection, prepared=False):
-        return self._machine.state
+        try:
+            name = value.state
+        except AttributeError:
+            name = value
+        return name
 
     def formfield(self, **kwargs):
         if self._machine: