I am using the google engine / python application. And I created my own data type and property. But I have a problem with that. This is my definition.
from datetime import datetime class MyDateTime(datetime): def __str__(self): return self.strftime("%Y-%m-%d %H:%M") class MyDateTimeProperty(db.Property):
and here is how I use it:
obj = MyClass(date=MyDateTime(2011,1,1)) obj.put()
and I got the following error message:
Traceback (most recent call last): File "C:\Program Files\Google\google_appengine\google\appengine\ext\admin\__init__.py", line 295, in post exec(compiled_code, globals()) File "<string>", line 23, in <module> File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1052, in put self._populate_internal_entity() File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1020, in _populate_internal_entity self._entity = self._populate_entity(_entity_class=_entity_class) File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1092, in _populate_entity self._to_entity(entity) File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1005, in _to_entity self.__set_property(entity, prop.name, prop.get_value_for_datastore(self)) File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 995, in __set_property entity[name] = datastore_value File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore.py", line 881, in __setitem__ datastore_types.ValidateProperty(name, value) File "C:\Program Files\Google\google_appengine\google\appengine\api\datastore_types.py", line 1477, in ValidateProperty 'Unsupported type for property %s: %s' % (name, v.__class__)) BadValueError: Unsupported type for property date: <class '__main__.MyDateTime'>
I searched for a solution for a while and could not solve it. Thanks in advance if anyone knows how to fix this.
thanks for the answer. I realized that the main problem is that the user data type cannot get into the data store, so I need to wrap / deploy the user data type to / from the regular data type using get_value_for_datastore
and make_value_from_datastore
this is my modified property definition:
class MyDateTimeProperty(db.Property):
The key is to use get_value_for datastore
to change the undirected MyDateTime object to a stored datetime object, and make_value_from_datastore
vice versa.