[google-appengine] Re: Shorthand to modify model-Objects?
On Mon, May 18, 2009 at 2:55 PM, Waldemar Kornewald wrote: > >> On Mon, May 18, 2009 at 1:52 PM, djidjadji wrote: >> >> > Can you use get_or_insert to update an object with the given key_name? > > If an object with that key_name already exists in the datastore it > won't be updated, so get_or_insert is not the right tool for the job. My mistake. In that case, if you want to replace all fields of an object, you can simply use the standard constructor-and-put method for new entities - it will overwrite the old one. If you only want to update some fields, you'll need to write your own method for this. -Nick Johnson --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: Shorthand to modify model-Objects?
I use a 'populate' method in a base model class. Slightly different from the previously posted function because it'll only update values of existing properties. def populate(self, **kargs): properties = self.properties() for key, value in kargs.iteritems(): if key in properties: setattr(self, key, value) I use it mostly to update an entity using data from a form. For example: entity = MyModel.get_by_key_name('my_key_name') entity.populate(**processed_form_data) entity.put() -- rodrigo --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: Shorthand to modify model-Objects?
> On Mon, May 18, 2009 at 1:52 PM, djidjadji wrote: > > > Can you use get_or_insert to update an object with the given key_name? If an object with that key_name already exists in the datastore it won't be updated, so get_or_insert is not the right tool for the job. Bye, Waldemar Kornewald --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: Shorthand to modify model-Objects?
Yes - that's exactly what it does. You can't use get_or_insert to update an object without a key_name, as it requires a unique identifier as the first argument. -Nick Johnson On Mon, May 18, 2009 at 1:52 PM, djidjadji wrote: > > Can you use get_or_insert to update an object with the given key_name? > > -- > **kwds > Keyword arguments to pass to the model class if an instance with the > specified key name doesn't exist. The parent argument is required if > the desired entity has a parent. > -- > > 2009/5/18 Nick Johnson (Google) : >> >> Hi Astrid, >> >> If your entities have key names, you can use the get_or_insert function: >> >> MyEnt.get_or_insert(key_name, title=title, name=name, public=True) >> >> -Nick Johnson >> >> On Sun, May 17, 2009 at 2:20 AM, astrid.thuec...@googlemail.com >> wrote: >>> >>> When one creates a new entity one can give all the parameters to the >>> constructor: >>> >>> MyEnt(title=title, name=name, public=True, ...) >>> >>> But if I modify an entity already stored I need to write it in single >>> lines: >>> me = MyEnt.all().filter("index =", index).fetch(1)[0] >>> me.title = title >>> me.name=name >>> me.public=False >>> ... >>> >>> is there a shorter way to do this? > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: Shorthand to modify model-Objects?
Can you use get_or_insert to update an object with the given key_name? -- **kwds Keyword arguments to pass to the model class if an instance with the specified key name doesn't exist. The parent argument is required if the desired entity has a parent. -- 2009/5/18 Nick Johnson (Google) : > > Hi Astrid, > > If your entities have key names, you can use the get_or_insert function: > > MyEnt.get_or_insert(key_name, title=title, name=name, public=True) > > -Nick Johnson > > On Sun, May 17, 2009 at 2:20 AM, astrid.thuec...@googlemail.com > wrote: >> >> When one creates a new entity one can give all the parameters to the >> constructor: >> >> MyEnt(title=title, name=name, public=True, ...) >> >> But if I modify an entity already stored I need to write it in single >> lines: >> me = MyEnt.all().filter("index =", index).fetch(1)[0] >> me.title = title >> me.name=name >> me.public=False >> ... >> >> is there a shorter way to do this? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: Shorthand to modify model-Objects?
Hi Astrid, If your entities have key names, you can use the get_or_insert function: MyEnt.get_or_insert(key_name, title=title, name=name, public=True) -Nick Johnson On Sun, May 17, 2009 at 2:20 AM, astrid.thuec...@googlemail.com wrote: > > When one creates a new entity one can give all the parameters to the > constructor: > > MyEnt(title=title, name=name, public=True, ...) > > But if I modify an entity already stored I need to write it in single > lines: > me = MyEnt.all().filter("index =", index).fetch(1)[0] > me.title = title > me.name=name > me.public=False > ... > > is there a shorter way to do this? > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[google-appengine] Re: Shorthand to modify model-Objects?
You can write a function that does it def updateAttr(obj, **kwds): if obj is None: return for k,v in kwds.iteritems(): setattr(obj,k,v) .. me = MyEnt.all().filter("index =", index).get() updateAttr(me, title = title, name=name, public=False) 2009/5/17 astrid.thuec...@googlemail.com : > > When one creates a new entity one can give all the parameters to the > constructor: > > MyEnt(title=title, name=name, public=True, ...) > > But if I modify an entity already stored I need to write it in single > lines: > me = MyEnt.all().filter("index =", index).fetch(1)[0] > me.title = title > me.name=name > me.public=False > ... > > is there a shorter way to do this? > > > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---