[google-appengine] Re: How to reference model property with a variable?
AFAIK you could just do this instead of doing an eval and setting the properties using setattr: m = p_model(**kwargs) where kwargs is the dictionary of properties that you are using to create your model instance. On Mon, Jun 8, 2009 at 2:33 PM, Ethan Post post.et...@gmail.com wrote: I figured this out, so no need to continue using copy. Been a while since I looked at Python. Sorry if these questions are obvious. Everything is working like a charm now. m = eval(p_model + '()') On Sun, Jun 7, 2009 at 5:45 PM, Ethan Post post.et...@gmail.com wrote: Now that I have the solution to my original problem I am trying to make a few improvements. I want to be able to pass a list of dictionaries or a single dictionary and multiple records in a single put. The code is working but I have a couple issues. One, I pass in p_model but I only use it to perform a copy, are there any known issues with using copy on a db.model object? Should I use deepcopy instead? Also, can I use getattr or setattr to instantiate an entity instance in Dict_To_Model2 instead of passing in the entity? I can't figure it out. This would be the line m = Foo(). Instead I would just pass in the name as a string. I wouldn't need to use copy if I could figure out how to do this. -- === 株式会社ビープラウド イアン・ルイス 〒150-0012 東京都渋谷区広尾1-11-2アイオス広尾ビル604 email: ianmle...@beproud.jp TEL:03-5795-2707 FAX:03-5795-2708 http://www.beproud.jp/ === --~--~-~--~~~---~--~~ 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: How to reference model property with a variable?
Now that I have the solution to my original problem I am trying to make a few improvements. I want to be able to pass a list of dictionaries or a single dictionary and multiple records in a single put. The code is working but I have a couple issues. One, I pass in p_model but I only use it to perform a copy, are there any known issues with using copy on a db.model object? Should I use deepcopy instead? Also, can I use getattr or setattr to instantiate an entity instance in Dict_To_Model2 instead of passing in the entity? I can't figure it out. This would be the line m = Foo(). Instead I would just pass in the name as a string. I wouldn't need to use copy if I could figure out how to do this. Thanks, Ethan class Foo (db.Model): name= db.StringProperty(required=False) phone = db.StringProperty(required=False) def Dict_To_Model2(p_model, p_dict): p = [] # We need a list if this is a dict. if not isinstance(p_dict, list): l = [p_dict] else: l = p_dict for i in l: l_model = copy.copy(p_model) for k in i: setattr(l_model, k, i[k]) p.append(l_model) db.put(p) class Call_Dict_To_Model(webapp.RequestHandler): def post(self): d = {key_name:unique_key123, name:Tim, phone:999} e = {key_name:unique_key456, name:Rat, phone:888} f = [d, e] m = Foo() Dict_To_Model2(m, f) q = Foo.all() for i in q: print i.name + ' ' + i.phone self.response.out.write('success=y') On Sat, Jun 6, 2009 at 9:56 AM, Ethan Post post.et...@gmail.com wrote: I want a method which takes a model object and a dictionary and adds a new record to the model using the the dictionary. The problem is I don't know how to refer to model.property using a variable. model(property) does not work. --~--~-~--~~~---~--~~ 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: How to reference model property with a variable?
Try http://docs.python.org/library/functions.html#setattr On Jun 6, 7:56 am, Ethan Post post.et...@gmail.com wrote: I want a method which takes a model object and a dictionary and adds a new record to the model using the the dictionary. The problem is I don't know how to refer to model.property using a variable. model(property) does not work. MyTable (db.model): location ... def Foo (p_model, p_dict): for i in p_dict: # Here is the tricky part, this does not work but you get the idea. p_model(i) = p_dict[i] p_model.put() def CallFoo: m = MyTable() d = {key_name:some_key_123, location:someplace} Foo(m, d) --~--~-~--~~~---~--~~ 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: How to reference model property with a variable?
value = getattr(model_instance, property_name) or setattr(model_instance, property_name, value) 2009/6/6 Ethan Post post.et...@gmail.com: I want a method which takes a model object and a dictionary and adds a new record to the model using the the dictionary. The problem is I don't know how to refer to model.property using a variable. model(property) does not work. MyTable (db.model): location ... def Foo (p_model, p_dict): for i in p_dict: # Here is the tricky part, this does not work but you get the idea. p_model(i) = p_dict[i] p_model.put() def CallFoo: m = MyTable() d = {key_name:some_key_123, location:someplace} Foo(m, d) -- It is better to be wrong than to be vague. — Freeman Dyson --~--~-~--~~~---~--~~ 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: How to reference model property with a variable?
Awesome. Thanks fellows. Works like a charm. On Sat, Jun 6, 2009 at 12:27 PM, David Wilson d...@botanicus.net wrote: value = getattr(model_instance, property_name) or setattr(model_instance, property_name, value) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---