Say I have a model like so:

class Settings(db.Model):
  country = db.StringProperty(required=True, choices=['us', uk',...])
  user = db.UserProperty(auto_current_user_add=True)

a form like so:

class SettingsForm(djangoforms.ModelForm):
  class Meta:
    model = Settings
    exclude = ['user']

Also (obviously) these settings are one per user. To guarantee uniqueness,
settings will have a key = 'settings:' + user.email().

Now I want to implement my request handler:

class Settings(webapp.RequestHandler):
  @login_required
  def get(self):
    user = users.get_current_user()
    settings = Settings.get_by_key_name('settings:' + user.email())
    if settings:
      settings_form = SettingsForm(instance=settings)
    else:
      settings_form = SettingsForm()
    -- render this stuff

  def post(self):
    settings_form = forms.SettingsForm(data=self.request.POST)
    if settings_form.is_valid():
      -- *** Problem here ***
    else:
      -- handle error case

At the point above marked as *** Problem here *** I do not know how to save
this entity properly. First I need to set the keyname, but I cannot do that
after the entity is created. Well I can if I set entity._key_name but that
is using private data that might change at some point in the future. Second,
I really need to do Model.get_or_insert() to avoid race conditions. So I
need to "unpack" the properties from the form and make a call to
Model.get_or_insert().

This seems like a recurring enough pattern. How do I do this the "right
way"?

Thanks in advance,

-- 

Alkis

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to