0

我需要訂單/購買,讓例如像正確使用重複屬性?

class Purchase(db.Model): 
    '''a completed transaction''' 
    item = db.ReferenceProperty(Item, repeated=True)) 

許多項目是重複的關鍵字法律和在這裏正確的方式使用?我的其他選擇是什麼?我應該用重複的stringproperty來代替嗎?

item_ids = model.StringProperty(repeated=True) 或重複KeyProperties? 感謝

回答

3

有一個ListProperty

class Purchase(db.Model): 
    items = db.ListProperty(db.Key) 

它的使用方式與Python列表:

p = Purchase() 
p.items = [item1.key(), item2.key(), item3.key()] 

在查詢但它看起來像一個單值字段。 ListProperty的每個值都單獨編制索引。下面

查詢將返回包含了item1所有購買:

Purchase.all().filter('items =', item1.key()) 

查詢下面將返回包含所有購買或者item1item2

Purchase.all().wilter('items IN', [item1.key(), item2.key()]) 

如果你永遠都需要索引更多比一個ListProperty不要忘記熟悉你的自我exploding-indexes

+4

嗨,尼克。 :-)混淆似乎在NDB和db之間。在NDB中,您使用repeat = True,而在db中使用ListProperty。 – 2012-01-30 01:19:59

+0

謝謝你的回答和評論。對於我的模型來說最自然的將是一個重複的Keyproperty,我會嘗試它的工作原理,因爲我喜歡NDB使用鍵和清晰語法的方式。 – 2012-01-30 05:45:36

+0

我甚至不知道'ndb'。非常感謝您的評論! – Ski 2012-01-30 07:57:12