Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Custom Property in Google App Engine
    primarykey
    data
    text
    <p>I'm using google app engine/python. And I have created a custom datatype and property. But I have problem using it. This is my definition</p> <pre><code>from datetime import datetime class MyDateTime(datetime): def __str__(self): return self.strftime("%Y-%m-%d %H:%M") class MyDateTimeProperty(db.Property): # specify datatype data_type = MyDateTime # For writing to datastore. def get_value_for_datastore(self, model_instance): return super(MyDateTimeProperty, self).get_value_for_datastore(model_instance) # For reading from datastore. def make_value_from_datastore(self, value): return super(MyDateTimeProperty, self).validate(value) class MyClass(db.Model): date = MyDateTimeProperty() </code></pre> <p>and this is how I use it:</p> <pre><code>obj = MyClass(date=MyDateTime(2011,1,1)) obj.put() </code></pre> <p>and I got the following error message:</p> <pre><code>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 "&lt;string&gt;", line 23, in &lt;module&gt; 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: &lt;class '__main__.MyDateTime'&gt; </code></pre> <p>I've searched for solution for a while and could not solve it. Thanks in advance if anyone knows how to fix it.</p> <hr> <p>thanks for the answer. I figured out that the prime problem is that custom data type can't go into datastore, so I need to wrap/unwrap the custom datatype to/from usual datatype using <code>get_value_for_datastore</code> and <code>make_value_from_datastore</code></p> <p>this is my modified property definition:</p> <pre><code>class MyDateTimeProperty(db.Property): # specify datatype data_type = MyDateTime # For writing to datastore. def get_value_for_datastore(self, model_instance): date = super(MyDateTimeProperty, self).get_value_for_datastore(model_instance) return datetime(date.year, date.month, date.day, date.hour, date.minute) # For reading from datastore. def make_value_from_datastore(self, value): if value is None: return None return MyDateTime(value.year, value.month, value.day, value.hour, value.minute) </code></pre> <p>The key is, using <code>get_value_for datastore</code> to change an unstorable MyDateTime object to storable datetime object, and <code>make_value_from_datastore</code> vice versa.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload