views:

64

answers:

1

Was wondering if I'm unconsciously using the Put method in my last line of code ( Please have a look). Thanks.

class User(db.Model):
  name = db.StringProperty()
  total_points = db.IntegerProperty()
  points_activity_1 = db.IntegerProperty(default=100)
  points_activity_2 = db.IntegerProperty(default=200)

  def calculate_total_points(self):
    self.total_points = self.points_activity_1 + self.points_activity_2

#initialize a user ( this is obviously a Put method )
User(key_name="key1",name="person1").put()

#get user by keyname
user = User.get_by_key_name("key1")

# QUESTION: is this also a Put method? It worked and updated my user entity's total points.
User.calculate_total_points(user)
+2  A: 

While that method will certainly update the copy of the object that is in-memory, I do not see any reason to believe that the change will be persisted to the the datastore. Datastore write operations are costly, so they are not going to happen implicitly.

After running this code, use the datastore viewer to look at the copy of the object in the datastore. I think that you may find that it does not have the changed total_point value.

Adam Crossland
yes you're right.I need to use db.put(user) after calculating the total points
fooyee