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 <[email protected]> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---