2011-07-13 98 views
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谷歌應用程序引擎 - 數據存儲 - GQL查詢

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

不工作?

回答

4

該查詢不起作用,因爲您傳遞的是"CatName"字符串而不是ProjectCategory實例密鑰。

從數據存儲檢索所需ProjectCategory實體先用:

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

第二種方法是使用隱式:

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

然後在這樣的查詢使用它作爲參數modelname_setProjectCategory實例的屬性:

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

pjc.project_set.fetch(10) #should contains some CatName projects 
+0

謝謝你,編輯後它工作正常。 –

相關問題