Google App Engine ndb 查询

1 投票
2 回答
3138 浏览
提问于 2025-04-19 09:37

我在谷歌云数据存储中创建了一个叫“事件”的实体。这个实体有一个由AppEngine生成的ID,还有两个属性:

  • 地点
  • 日期

我想通过它的ID(5629499534213120)来查询这个实体,所以这是我的代码。

    key = 5629499534213120
    e = Event.get_by_id(key)
    logging.info("Event Location = %s" % e.Location)

变量e的值是NoneType,也就是没有值。

代码

   __author__ = 'vinayjoseph'

from google.appengine.ext import ndb
import logging


class Event(ndb.Model):
    """Models an individual event at xxx xxxx """
    Date = ndb.DateTimeProperty()
    Location = ndb.StringProperty()


def get_meeting_date():
    """gets the next meeting date from the No SQL Schemaless Google Datastore
    """
    key = 5629499534213120
    e = Event.get_by_id(key)
    logging.info("Event Location = %s" % e.Location)

e是NoneType

在数据存储中,我看到以下内容:
https://console.developers.google.com/project/apps~xxxxx/datastore/editentity?key=xxxxx%2FEvent%20id:5629499534213120

数据存储中实体的截图

我怀疑问题可能出在我的密钥上。

当我在开发环境中使用dev_appserver.py查询数据存储时,它是可以工作的。我在开发环境中使用的是不同的密钥。

def get_meeting_date():
    """gets the next meeting date from the No SQL Schemaless Google Datastore
    """
    #dev
    key = 6401356696911872
    #prd
    #key = 5629499534213120
    e = Event.get_by_id(key)
    logging.info("Event Location = %s" % e.Location)

在这里输入图片描述

2 个回答

0

在查询方法中,你使用了祖先。如果你的实体有父级,那么在调用 get_by_id 的时候,你需要把它包含进去。

2

好的,我终于搞明白了。

我需要对实体本身做一些修改,才能正确过滤数据。所以现在的属性如下所示。

实体属性

下面是用Python写的代码:

__author__ = 'vinayjoseph'

from google.appengine.ext import ndb
import logging
from datetime import datetime

class Event(ndb.Model):
    """Models an individual event at xxx xxx """
    Date = ndb.DateTimeProperty()
    Location = ndb.StringProperty()
    Address = ndb.StringProperty()
    Name = ndb.StringProperty()


def get_meeting_date():
    """gets the next meeting date from the No SQL Schemaless Google Datastore
    """
    qry = Event.query(Event.Name == 'Next Meeting Location')
    for event in qry.fetch(1):
        logging.info("Meeting is on %s at %s" % (str(event.Date), event.Address))

而且效果非常好。可以看看应用引擎中的日志记录。

在这里输入图片描述

撰写回答