简体   繁体   中英

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 +: 'IntegerProperty' 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. Xy refers to the unbound property on the class object. xy refers to the bound property, and indeed in the above example print xy + 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM