Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按实体过滤查询';应用程序引擎中的子对象(python)_Python_Google App Engine_Google Cloud Datastore - Fatal编程技术网

按实体过滤查询';应用程序引擎中的子对象(python)

按实体过滤查询';应用程序引擎中的子对象(python),python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,我有一个问题模型和一个回答模型,其家长设置了一个特定的问题,如: class Question(db.Model): myQuestion = db.StringProperty() class Response(db.Model): responder = db.ReferenceProperty(reference_class = User, collection_name = 'my_responses') myResponse = db.StringProperty()

我有一个问题模型和一个回答模型,其家长设置了一个特定的问题,如:

class Question(db.Model):
  myQuestion = db.StringProperty()

class Response(db.Model):
  responder = db.ReferenceProperty(reference_class = User, collection_name = 'my_responses')
  myResponse = db.StringProperty()

def createQuestion(self, user, question):
  Question(myQuestion = question, parent = user).put()

def respond(self, user, question, response):
  Response(responder = user, myResponse = response, parent = question).put()

给定一个用户,如何获取用户未回答的所有问题?

一个解决方案是保留用户已回答问题的列表。您可以将这些列表保存在用户的实体组中,并使用当前月份(例如)作为关键字名称

class QuestionsAnswered(db.Model):
    answered = db.ListProperty(db.Key)
将QuestionsAnswered实体的密钥名称设置为当前月份(我喜欢'201106',对于Instance),并将它们放入用户的实体组中。这将使获取所需的SEED列表变得容易

要获得看不见的问题列表,可以执行以下操作:

question_query = Questions.all().order('asked_date')
questions = questions.fetch(50)

if not questions:
  # return ... nothing to do here

month = questions[0].asked_date.strftime('%Y%m')
answered_list = QuestionsAnswered.get_by_key_name(month, parent=user)

question_keys = [question.key() for question in questions]
new_question_keys = set(question_keys) - set(answered_list.answered)

您可以扩展此选项以检查返回问题中最早的月份。如果没有返回足够的问题,您也可以将此扩展到。Robert Kluin建议,

,您应该考虑向问题实体添加一个“响应”属性。你仍然可以像这样达到同样的效果。但这是解决你问题的一个糟糕的办法

questions=Question.all().ancestor(user)
result = []
for question in questions:
    responded = Response.all().ancestor(question).get()
    if responded is not None:
       result.append(responded)

除了回答之外,还有什么能将问题与用户联系起来的吗?我还将问题的父项设置为用户。对不起,我没有具体说明。