This would be better done with a property, I think:
class Label(db.Model):
name = db.StringProperty(required=True)
def get_id(self):
return self.key().id()
id = property(get_id)
Then you can do:
label = Label(name='foo')
label.put()
print label.id
You could put more intelligence into get_id() of course.
I'm not sure there's a compelling reason to do any of this, though,
since label.key().id() is quite simple if you want a simple numeric id.
Regards,
Jeremey.
An auto-incremented id is equal to the count only as long as you never
delete anything. :)
Here's a thought:
class Foo(db.Model):
name = db.StringProperty()
counter = db.ReferenceProperty(Counter, required=True)
class Counter(db.Model):
count = db.IntegerProperty()
def incr(self):
db.run_in_transaction(self.__increment)
def decr(self):
db.run_in_transaction(self.__decrement)
def __increment(self):
counter = Counter.get(self.key())
counter.count += 1
self.count = counter.count
counter.put()
def __decrement(self):
counter = Counter.get(self.key())
counter.count -= 1
self.count = counter.count
counter.put()
# to initialize once and only once
foo_count = Counter(count=0, key_name="foo_counter")
foo_count.put()
# Then:
f = Foo(name="some_foo", counter=foo_count)
f.put()
f.counter.incr()
Regards,
Jeremey.
FWIW, the relationship here is not required, you could do without it.
It might have its uses though, depending on what you're doing.
Regards,
Jeremey.
So, what is the advantage over doing this in the class that you wanted
unique identifiers in?
--
Brett Morgan http://brett.morgan.googlepages.com/
Regards,
Jeremey.