为什么我的结构化 属性 查询因 BadFilterError 而失败?

Why is my query for a structured property failing with BadFilterError?

我不明白如何filter for structured properties

我一直到那里错误:

BadFilterError: invalid filter: Cannot query for unindexed property author.email.  

我正在尝试在我的测试设置中创建一个实体。

我正在使用 GAE tutorial 中的代码。

这是模型:

class Author(ndb.Model):
    """Sub model for representing an author."""
    identity = ndb.StringProperty(indexed=False)
    email = ndb.StringProperty(indexed=True)


class Greeting(ndb.Model):
    """A main model for representing an individual Guestbook entry."""
    author = ndb.StructuredProperty(Author)
    content = ndb.StringProperty(indexed=False)
    date = ndb.DateTimeProperty(auto_now_add=True)

这是简单的查询:

Author.query( Author.email == "bryan@mail.com").get()

这是我在 functional_tests.py 中的测试代码:

import sys, os, subprocess, time, unittest, shlex   
sys.path.append("/usr/local/google_appengine")    
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")    
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")    
sys.path.append("/usr/local/google_appengine/lib/django-1.5")    
sys.path.append("/usr/local/google_appengine/lib/cherrypy")    
sys.path.append("/usr/local/google_appengine/lib/concurrent")    
sys.path.append("/usr/local/google_appengine/lib/docker")    
sys.path.append("/usr/local/google_appengine/lib/requests")    
sys.path.append("/usr/local/google_appengine/lib/websocket")    
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")    
sys.path.append("/usr/local/google_appengine/lib/antlr3")    

from selenium import webdriver    
from google.appengine.api import memcache, apiproxy_stub, apiproxy_stub_map   
from google.appengine.ext import db    
from google.appengine.ext import testbed    
from google.appengine.datastore import datastore_stub_util     
from google.appengine.tools.devappserver2 import devappserver2    


class NewVisitorTest(unittest.TestCase):    

    def setUp(self):    
        # Start the dev server  
        cmd = "/usr/local/bin/dev_appserver.py /Users/Bryan/work/GoogleAppEngine/guestbook/app.yaml --port 8080 --storage_path /tmp/datastore --clear_datastore --skip_sdk_update_check"  
        self.dev_appserver = subprocess.Popen(shlex.split(cmd),   
                                              stdout=subprocess.PIPE)  
        time.sleep(2) # Important, let dev_appserver start up  

        self.testbed = testbed.Testbed()
        self.testbed.setup_env(app_id='dermal')    
        self.testbed.activate()    

        self.testbed.init_user_stub()    
        # Create a consistency policy that will simulate the High Replication consistency model.
        # with a probability of 1, the datastore should be available.
        self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
        # Initialize the datastore stub with this policy.
        self.testbed.init_datastore_v3_stub(datastore_file="/tmp/datastore/datastore.db", use_sqlite=True, consistency_policy=self.policy)    
        self.testbed.init_memcache_stub()    
        self.datastore_stub = apiproxy_stub_map.apiproxy.GetStub('datastore_v3')            
        # setup the dev_appserver    
        APP_CONFIGS = ['app.yaml']    

        # setup client to make sure   
        from guestbook import Author, Greeting  
        if not ( Author.query( Author.email == "bryan@mail.com").get()):  
            logging.info("create Admin")  
            client = Author(  
            email = "bryan@mail.com",   
            ).put()
            Assert( Author.query( Author.email == "bryan@mail.com").get() )
        self.browser = webdriver.Firefox()    
        self.browser.implicitly_wait(3)    

    def tearDown(self):    
        self.browser.quit()    
        self.testbed.deactivate()    
        self.dev_appserver.terminate()  

    def test_submit_anon_greeting(self):
          self.browser.get('http://localhost:8080')
          self.browser.find_element_by_name('content').send_keys('Anonymous test post')
          self.browser.find_element_by_name('submit').submit()
          Assert.assertEquals(driver.getPageSource().contains('Anonymous test post'))

您正在查询 Author 模型。但是在您的结构中,作者仅作为问候语的一部分存在。因此,您应该创建一个 Greeting 并查询该模型。

Greeting(
    author=Author(email="bryan@mail.com"),
    content="Hello there!")
).put()

Greeting.query(Greeting.author.email=="bryan@mail.com").get()