過濾

2012-08-23 47 views
0

我有以下的模型,其中不同的文本標記,以文本的不同的集合,每個文本可以有幾個版本,但只有一個「活動」版:過濾

class Collection(db.Model): 
    name = db.StringProperty() 
    ...  

class Text(db.Model): 
    title = db.StringProperty(default="Untitled") 
    ... 

class Version(db.Model): 
    text = db.ReferenceProperty(Text, collection_name="versions") 
    content = db.TextProperty() 
    active = db.BooleanProperty(default=True) 
    ... 

class Tag(db.Model): 
    collection = db.ReferenceProperty(Collection, collection_name="c_tags") 
    text = db.ReferenceProperty(Text, collection_name="t_tags") 

現在我喜歡一個集合來顯示所有的「活躍」的文本:

class ViewCollection(webapp.RequestHandler): 
    def(get): 
     collection = Collection.get(self.request.get("key")) 
     # grabs the collection to display 
     tags = collection.t_tags 
     # grabs all the tags linking a text and a collection 
     texts = [t.text for t in tags] 

這會給我所有的文字列表,這樣我就可以輕鬆打印所有文字標題(t.title for t in texts),但沒有任何聰明的辦法現在我可以抓取相應的「活動」版本的ea ch文本?

或者這種類型的過濾不同的模型類是不可能的?

回答

1

您必須在Version對象中發出另一個查詢,其中text == t.text和active == True。

根據您的使用情況,將標準中的最新版本非標準化並存儲起來可能是值得的,因此您不需要額外的查詢來獲取數據。這種非規範化是nosql數據存儲的常見「優化」。

1

App Engine的數據存儲區不針對這種關係代數問題而設計。我不相信用你目前的設計可以高效地完成。我建議你閱讀NoSQL數據庫概念,並將你的模型重新設計爲「不太關係」。