3

我希望能夠給定數據存儲區中模型的密鑰,只需設置referenceproperty而不加載模型本身(因爲我不需要那個信息)。如何使用只有一個密鑰設置ReferenceProperty,而不是獲取模型

例如:

class Book(db.Model): 
    author = db.StringProperty() 
    title = db.StringProperty() 

class Review(db.Model): 
    book = db.ReferenceProperty(Book) 
    description = db.StringProperty() 

假設我已經有鑰匙的Book(稱之爲bookKey),但我沒有相應的Book對象本身,有沒有辦法做相當於的

review = Review() 
review.description = "It was ok, but I would recommend it for insomniacs." 
review.book = bookKey 

或者我需要

book = Book.get(bookKey) #I don't want this datastore access 
         #as I don't need the book object. 
review = Review() 
review.description = "It was ok, but I would recommend it for insomniacs." 
review.book = book 

我已經找到了使用get_value_for_datastoreReferenceProperty中提取密鑰和ID的方法,但是我在「設置」之後。

回答

5

review.book = bookKey會正常工作,並設置引用屬性而不提取模型。

+0

謝謝。這正是我所需要的(並且將字符串表示再次用作Key)。 – jrnewman42 2011-04-01 00:05:13

相關問題