以下是在app引擎数据存储区中获取唯一ID的正确方法

时间:2015-06-06 16:36:19

标签: python google-app-engine

我有一个遗留的Google App引擎代码,它在Python中具有以下实体类

class AffiliateParent(db.Model):
    name = db.StringProperty(required = True)

class Affiliate(db.Model):
    email = db.StringProperty(required = True)
    point_gain = db.IntegerProperty()
    point_used = db.IntegerProperty()
    #feature_upgrade = db.ListProperty(str)
    modified_time = db.DateTimeProperty(auto_now = True)

我很想知道,以下是生成唯一ID的正确方法吗?它们在Affiliate表中是否保证唯一?

affiliate_parent_key = AffiliateParent.all(keys_only=True).filter('name =', 'yancheng').get();
affiliate_parent = db.get(affiliate_parent_key);
# check whether affiliate exist, if not create one
affiliate_parent = db.get(affiliate_parent_key);

q = Affiliate.all()
q.ancestor(affiliate_parent)
q.filter('email =', email)
affiliate = q.get()

if not affiliate:
    affiliate = Affiliate(
        email = email,
        point_gain = 0,
        point_used = 0,
        parent = affiliate_parent
    )
    affiliate.put()

# 4503602445942784
# is this unique?
unique_id = affiliate.key().id()

1 个答案:

答案 0 :(得分:1)

是的,如果您在实例化模型时未提供set pos to "300, 300" set t to quoted form of "AB" do shell script "/usr/bin/python <<END import binascii, sys, time import Quartz.CoreGraphics as qcg def mouseEvent(type, lrBtn, p): theEvent = qcg.CGEventCreateMouseEvent(None, type, (p), lrBtn) qcg.CGEventPost(qcg.kCGHIDEventTap, theEvent) time.sleep(0.3) def mousemove(p): mouseEvent(qcg.kCGEventMouseMoved, qcg.kCGMouseButtonLeft, p) def rightClick(lrBtn, p): mouseEvent(qcg.kCGEventRightMouseDown, lrBtn, p) mouseEvent(qcg.kCGEventRightMouseUp, lrBtn, p) def leftClick(lrBtn, p): mouseEvent(qcg.kCGEventLeftMouseDown, lrBtn, p) mouseEvent(qcg.kCGEventLeftMouseUp, lrBtn, p) tString=" & t & " pos=" & pos & " currentpos=qcg.CGEventGetLocation(qcg.CGEventCreate(None)) t = (bin(int(binascii.hexlify(tString),16)))[2:] ascText = -len(t) % 8 * '0' + t for i in range(0, len(ascText), 8): ### to every 8 digit ascT = '1' + (ascText[i:i+8])[::-1] + '10' for c in ascT: if (c=='1'): rightClick(qcg.kCGMouseButtonRight, pos) else: leftClick(qcg.kCGMouseButtonLeft, pos) time.sleep(3); mousemove(currentpos) # Restore mouse position END" id,那么数据存储区将生成一个唯一ID,并在您key时将其分配给您的实体。因此.put()将是唯一的

您还可以使用affiliate.key.id()

生成唯一ID

https://cloud.google.com/appengine/docs/python/datastore/functions#allocate_ids