views:

387

answers:

1

Has anyone tried to dynamically select which properties they want to write to an entity on appengine? For example:

I have a web form with 5 fields, and any given user will fill out some subset of those fields. I POST only the fields with data to the server (e.g. Fields 1,2,4). On the server side, how do I elegantly write only properties 1,2, and 4? The Model class has a function that returns a dictionary of property names (Model.properties()), but how would I use it to select property names?

In SQL, I would build an INSERT or UPDATE statement by matching the fields POSTed against the Model.properties() dictionary. I would look at the db module code in the Appengine SDK, to see if the Model class had some collection of Property objects, but I can't find the module on my disk (I'm a little new to python and appengine).

Update: I read trunk/google/appengine/ext/db/init.py which confirmed that there is no way to refer to the properties as a group. Anyone know of a workaround?

Any thoughts?

Update2: This question was answered on the Google Group for AppEngine: http://groups.google.com/group/google-appengine/browse_thread/thread/b50be862f6d94b6e#

+1  A: 

The python module will look something like this:

from google.appengine.ext.db import Key
from google.appengine.api.datastore import Get, Put

def edit_item(request, db_id):

    objKey = Key(str(db_id))

    if request.method == 'POST':
        objEntity = Get(objKey)
        for k, v in request.POST.iteritems():
             objEntity[k]=v
        Put(objEntity)
        return HttpResponseRedirect('/')

    query = TestModel.get(objKey)
    return render_to_response('edit.html', ({'modify_data': query,}))

Your HTML should look something like this:

<form method="POST" action="." enctype="multipart/form-data">
  Title: <input type="text" name="title" value="{{modify_data.field1}}"/>
  Text: <input type="text" name="txt" value="{{modify_data.field2}}"/>

  <input type="submit"/>
</form>