Simply you have to put this on your model:

class MyModel(db.Model):
  ...
  mydata = JSONProperty()
  ...

And write mydata as you want, for example:

mymodel.mydata = {
  'hello': 'world',
  'bye': 'world',
}

The same for reading:

mymodel.mydata['hello'] # == 'world'
mymodel.mydata['bye'] # == 'world'


This is the code for JSONProperty:

from google.appengine.ext import db
import simplejson

class JSONProperty(db.TextProperty):
  def validate(self, value):
    return value

  def get_value_for_datastore(self, model_instance):
    result = super(JSONProperty, 
self).get_value_for_datastore(model_instance)
    result = simplejson.dumps(result)
    return db.Text(result)

  def make_value_from_datastore(self, value):
    try:
      value = simplejson.loads(str(value))
    except:
      pass

    return super(JSONProperty, self).make_value_from_datastore(value)

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-appengine/-/1nhX-igI1nsJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to