简体   繁体   English

Google App Engine NDB查询

[英]Google App Engine ndb query

I have created an Entity, called "Event" in the Google Cloud Datastore for my project. 我已经在我的项目的Google Cloud数据存储区中创建了一个名为“事件”的实体。 The entity has an ID generated by AppEngine followed by two properties 实体具有由AppEngine生成的ID,后跟两个属性

  • Location 位置
  • Date 日期

I am trying to query this entity by its ID (5629499534213120), so , here is my code. 我正在尝试通过其ID(5629499534213120)查询该实体,因此,这是我的代码。

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

The value of e is NoneType. e的值为NoneType。

code

   __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 is NoneType e是NoneType

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

数据存储中实体的屏幕截图

I suspect the problem might be with my key. 我怀疑问题可能出在我的钥匙上。

When I try to query the datastore in development using dev_appserver.py it works. 当我尝试使用dev_appserver.py查询开发中的数据存储时,它可以工作。 I am using a different key for dev. 我为开发人员使用了其他密钥。

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)

在此处输入图片说明

OK so I finally figured it out. 好,所以我终于知道了。

I had to make a few changes to the entity itself, just to get the filtering right. 为了正确过滤,我不得不对实体本身进行一些更改。 So the new properties are as seen below. 因此,新属性如下所示。 实体属性

The code in python is as follows: 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))

And it works like a charm. 它就像一种魅力。 Check out the log entry in app-engine 在app-engine中查看日志条目

在此处输入图片说明

In the query method, you're using ancestor. 在查询方法中,您使用的是祖先。 If your entities have a parent, then you have to include it in the get_by_id call. 如果您的实体有父母,那么您必须将其包含在get_by_id调用中。

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

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