1
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

2
  • 2
    Integers get auto-promoted to longs in python. And that has nothing to do with the error. Commented Jan 5, 2011 at 23:45
  • What are you trying to achieve? Aside from the errors pointed out by Thomas, trying to use global counters is usually a bad idea in App Engine (or any other distributed system). Commented Jan 6, 2011 at 2:39

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This is correct. In fact, it's the only complete and correct answer yet posted, so I'm not sure why it was voted down.
Because it's completely wrong. You don't need an initialization function, you don't need to execute a query or request an object from the datastore, and you don't need to resave the instance. The ONLY thing you need to do is tweak a single line, and the code works as written.
@Chris: It might run with a simple tweak, but it won't achieve what the poster is trying to do. The question specifically describes getting a number from the datastore, adding one, and putting it back in the datastore.
When someone asks "How do I drive to the store to buy milk? My car won't start", it's not helpful to provide a map and turn-by-turn directions if they're missing spark plugs.
@Chris: The code has multiple issues. The distinction between Config and config is the most obvious, because it raises an error. But the lack of any code to store to or retrieve from the datastore is really the most significant problem, so it's wrong to suggest that it's a single line fix. To stretch the analogy, it's like fitting a spark plug to a car while ignoring the lack of, say, wheels.
1

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.

Comments

0

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.

2 Comments

That's not true of numbers. 0.1 + 2 adds just fine.
I said compatible type. float and ints are compatible.
0

http://code.google.com/appengine/articles/sharding_counters.html

This is a basic conter that can be updated multiple times a second. Works perfect

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.