简体   繁体   English

访问Google App Engine数据存储区中实体的数字ID

[英]Access the numeric ID for an entity in google app engine's datastore

I've taken a look at this topic , on accessing id from datastore. 我已经看过有关从数据存储区访问ID的主题 Now I tried doing something like post.key().id() but it does not return the Numeric ID that I want. 现在,我尝试做类似post.key().id()但是它没有返回我想要的数字ID。

How does one access the numeric ID of an entity in google datastore. 如何访问Google数据存储区中实体的数字ID。 I am using GAE 1.8 wiht Python 2.7. 我正在使用带有Python 2.7的GAE 1.8。 I believe a lot has changed from the time of the question that I just showed you. 我相信,从我刚刚向您展示的问题开始,发生了很多变化。

This is all an attempt to make a permalink for my entity called post , which is a blog post. 这都是为我的名为post实体建立永久链接的尝试,这是博客文章。

Here is the code: 这是代码:

# coding=utf-8
from google.appengine.ext import db


class BlogPost(db.Model):
    title = db.StringProperty(required=True)
    content = db.TextProperty(required=True)
    created_at = db.DateTimeProperty(auto_now_add=True)

    def url(self):
        return '/blog/%s' % self.key().id()

Any help regarding this would be greatly appreciated. 关于此的任何帮助将不胜感激。

What are you expecting this is the method to get the numeric id. 您期望这是获取数字ID的方法。

If you get None then it means you have created the entity using a string for the id. 如果您获得None(无),则意味着您已经使用ID的字符串创建了实体。

You could try 你可以试试

key().id_or_name() Returns the name or numeric ID of the data entity, whichever it has, or None if the entity has neither a name nor a numeric ID. key().id_or_name()返回数据实体的名称或数字ID,以其拥有者为准;如果该实体既没有名称也没有数字ID,则返回None。

If you get None from id_or_name() it means the entity has yet to be put() 如果从id_or_name()获得None,则表示该实体尚未put()

When you create an entity, you specify an ancestor key, and one of either a string name, or a numeric id. 创建实体时,您可以指定祖先键,以及字符串名或数字ID之一。

If you create it with a string, there is no numeric id. 如果使用字符串创建,则没有数字ID。

You probably want name(), or if you're not sure id_or_name() 您可能需要name(),或者不确定id_or_name()

Here are the docs: https://developers.google.com/appengine/docs/python/datastore/keyclass#Key_id_or_name 以下是文档: https//developers.google.com/appengine/docs/python/datastore/keyclass#Key_id_or_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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