I am continuing to learn how to integrate Flex to Google App Engine. Now my test project is able to update objects, in addition to inserting and retrieving them.
On the server side, this is done by the update service operation:
def update(self, project):
logging.debug('udpate %s' % (project))
existing_project = Project.get(project._key)
existing_project.name = project.name
existing_project.put()
return Project.get(project._key)
There is also a new save operation that decides between insert and update depending on the state of the object. If the object was read before, it is updated. Otherwise, it is inserted:
def save(self, project):
logging.debug('save %s' % (project))
if hasattr(project, '_key') and project._key != None:
return self.update(project)
else:
return self.insert(project)
The App Engine model is also saving the date and time each object was last modified:
class Project(db.Model): # ... modified_at = db.DateTimeProperty(auto_now=True)
On the client side, the save operation is called after an object is prepared. If the object was being edited, its key was saved and so the service will know it must be updated. If it is a new object, the key will be null so the service will insert a new object.
public function submitProject():void {
var newProject:Object = new Object();
newProject.code = project_code.text;
newProject.name = project_name.text;
newProject._key = project._key;
gateway.call("ProjectService.save", new Responder(null, onFault), newProject);
getProjects();
}
The _key property is a very important piece of information in this context. So I patched the PyAMF library to include it within every object it sends to the client. There is a ticket for that on the PyAMF project.
def writeObjectAMF(self, obj, args, kwargs, remove): """ Writes an object that has already been prepared by writeObjectAMF0 or writeObjectAMF3. """ try: obj._key = str(obj.key()) except: obj._key = None self.writeObject(obj, *args, **kwargs) del obj._key if remove: self.context.class_aliases[obj.__class__] = None
All the source code for this project is hosted in github. Please be aware that I am sharing what I find out as I learn. I am sure there are better ways of using this tools. I hope to learn them. Comments are welcome.

PyAMF and Google App Engine…
After Google’s announcement on Monday we received a lot of questions about running PyAMF on the Google App Engine so we decided to start working on a tutorial. This is a work in progress and might require some updates to the library. We’re…
Trackback by PyAMF blog — July 20, 2008 @ 5:49 pm