为什么模型的 key_name 在 google-app-engine 上没有 key().id()

发布于 2024-09-10 20:19:20 字数 590 浏览 1 评论 0原文

如果我使用这个:

class A(db.Model):
    a=db.StringProperty()

class demo(BaseRequestHandler):
    def get(self):
        a=A()
        a.a='sss'
        a.put()
        raise Exception(a.key().id())

我可以得到 a.key().id() 是 961

但如果我添加 key_name="aaa" , a.key().id() 将是 None :

class A(db.Model):
    a=db.StringProperty()

class demo(BaseRequestHandler):
    def get(self):
        a=A(key_name="aaa")
        a.a='sss'
        a.put()
        raise Exception(a.key().id())

那么我怎样才能得到key().id() 当我设置 key_name 时

谢谢

if i use this :

class A(db.Model):
    a=db.StringProperty()

class demo(BaseRequestHandler):
    def get(self):
        a=A()
        a.a='sss'
        a.put()
        raise Exception(a.key().id())

i can get the a.key().id() is 961

but if i add key_name="aaa" , the a.key().id() will be None :

class A(db.Model):
    a=db.StringProperty()

class demo(BaseRequestHandler):
    def get(self):
        a=A(key_name="aaa")
        a.a='sss'
        a.put()
        raise Exception(a.key().id())

so how can i get the key().id() when i set a key_name

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2024-09-17 20:19:20

你不能,因为它们是同一件事。

实体具有编码字符串键加上整数 ID 或字符串名称这一事实可能会产生误导性印象,即引用实体的各种方式是重叠或冗余的。他们不是。

键名就像文件系统中的文件名。 ID 就像系统自动选择的文件名。密钥本身就像文件的完整路径,包括目录。

考虑 Key.from_path 方法:

k = Key.from_path('User', 'Boris', 'Address', 9876)

kind=User&name= Boris 就像一个目录,而 kind=Address&name=9876 就像包含您的实体的文件。返回的密钥只是该路径的编码版本。

App Engine 依赖于每个实体都有一个固定的、不可变的路径,因此只有一个密钥。如果一个实体可以由用户分配的名称系统分配的 ID 表示,则这意味着具有 n 个祖先的单个实体可以有 2^(n +1)不同的路径和键。

You can't, because they're the same thing.

The fact that entities have an encoded string key plus either an integer ID or a string name can give the misleading impression that the various ways to refer to an entity are overlapping or redundant. They're not.

A key name is like a filename within a filesystem. An ID is like a filename that the system has picked automatically. The key itself is like the full path to the file, including directories.

Consider the Key.from_path method:

k = Key.from_path('User', 'Boris', 'Address', 9876)

kind=User&name=Boris is like a directory, and kind=Address&name=9876 is like a file containing your entity. The key returned is just an encoded version that path.

App Engine relies on each entity have one fixed, immutable path, ergo one key. If an entity could be represented by both a user-assigned name and a system-assigned ID, this would mean that a single entity with n ancestors could have 2^(n+1) different paths and keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文