In Appengine, I try to get a property value calculated automatically and saved with the object.
I have a class, Rectangle, and it has width, height and area. Obviously, a region is a function of width and height, but I want it to be a property because I want to use it for sorting. Therefore, I am trying to modify the put () function to sneak an area while saving a Rectangle as follows:
class Rectangle(db.Model): width = db.IntegerProperty() height = db.IntegerProperty() area = db.IntegerProperty() def put(self, **kwargs): self.area = self.width * self.height super(Rectangle, self).put(**kwargs)
This works when I call put()
directly on the Area object:
re1 = Rectangle(width=10, height=10) re1.put() print re1.area
But when I use db.put()
(for example, to save a lot of them at once), it breaks.
re2 = Rectangle(width=5, height=5) db.put(re2) print re2.area
What is the correct way to color a calculated value?
source share