Google App Engine Python adding
class Config(db.Model):
Latest = db.IntegerProperty()
class New(webapp.RequestHandler):
def get(self):
config = Config()
Last = Config.Latest
t = Last + 1
returns
t = LastUUID + ADDNUM
TypeError: unsupported operand type(s) for +: 'Intege开发者_如何转开发rProperty' and 'Int'
What im trying to do is get the int from the datastore and app 1 to it. Then reassign the int in the datastore. I have no clue why it is throwing these errors. I even tried t = int(Last +1)
.
UPDATE: This is what i needed and solved my problem. http://code.google.com/appengine/articles/sharding_counters.html
The changes you need are more than tweaking a single line.
You need some sort of initialisation function which will create a Config()
instance, assign Latest
an initial value (like 1 or 0) and store it in the datastore.
Then your RequestHandler needs to execute a query to retrieve the relevant Config
instance. Finally, update Latest
, and save the instance into the datastore again.
This has nothing to do with Google App Engine or Django. In the following code:
class X(object):
@property
def y(self):
return 5
x = X()
print X.y + 5
... you get the same error. X.y
refers to the unbound property on the class object. x.y
refers to the bound property, and indeed in the above example print x.y + 5
prints "10".
Change the line to Last = config.Latest
and it should work. And I strongly recommend you take up the recommendations in PEP 8, particularly under "Prescriptive: Naming Conventions". Generally speaking, in Python classes should use CapWords
, while functions and variables should use lowercase_with_underscores
.
The syntax you're looking for is
t = int(Last) +1
However, I'm not sure that's what you want to do. I don't know django or whatever framework this is so it's hard to say. But in python, types must be compatible for you to add them.
http://code.google.com/appengine/articles/sharding_counters.html
This is a basic conter that can be updated multiple times a second. Works perfect
精彩评论