2
class ProjectCategory(db.Model):
    name = db.StringProperty("Category name", required = True)

    def __str__(self):
        return str(self.name)

class Project(db.Model):
    name = db.StringProperty("Name", required = True)
    category = db.ReferenceProperty(ProjectCategory)  
    description = db.TextProperty("Description", required = True)
    #file_name = db.StringProperty("File name", required = True)
    file = db.BlobProperty("Image")

    whenstarted = db.DateTimeProperty("Start time")
    whenended = db.DateTimeProperty("End Time")

    def __str__(self):
        return str(self.title)

如何获取类别为 CatName 的所有项目

db.GqlQuery("SELECT * FROM Project WHERE category = :1", "CatName")

不工作?

4

1 回答 1

4

该查询不起作用,因为您传递的是"CatName"字符串而不是ProjectCategory实例的键。

ProjectCategory首先从 Datastore 中检索您想要的实体:

pjc =  GqlQuery("SELECT * FROM ProjectCategory WHERE name = :1", "CatName").get()

然后将其用作查询中的参数,如下所示:

db.GqlQuery("SELECT * FROM Project WHERE category = :1", pjc.key())

第二种方法是使用实​​例的隐式modelname_set属性ProjectCategory

pjc =  GqlQuery("SELECT * FROM ProjectCategory WHERE name = :1", "CatName").get()

pjc.project_set.fetch(10) #should contains some CatName projects 
于 2011-07-13T14:21:22.183 回答