[英]Get name of subclass of ndb.StructuredProperty(subclass, repeated=True)
[英]ndb.StructuredProperty not calling ndb.PolyModel subclass methods
當將ndb.Polymodel超類存儲為ndb.StructuredProperty時,我無法訪問子類方法。 而是調用超類方法,並引發NotImplementedError。 這是我要完成的工作的簡要版本。
class Recipient(polymodel.PolyModel):
name = ndb.StringProperty()
def PrettyPrinting(self):
raise NotImplementedError, 'Rawr'
class ShippingRecipient(Recipient):
address_line_one = ndb.StringProperty()
#there are other properties, but they aren't necessary here.
def PrettyPrinting(self):
return 'Hey, this should be called.'
class LocalRecipient(Recipient):
distribution_location = ndb.StringProperty()
#same deal, more attributes, but useless for this example.
def PrettyPrinting(self):
return 'Hey this should be called.'
class Shipment(ndb.Model):
recipient = ndb.StructuredProperty(Recipient)
現在說我已經保存了一個貨件,並將一個ShippingRecipient存儲在貨件的收件人字段中。 在數據存儲區中,收貨收件人.class == ['Recipient','ShippingRecipient']。 當我打電話時:
shipment = Shipment.get_by_id('some_key')
shipment.recipient.PrettyPrinting()
引發NotImplementedError而不是PrettyPrinting(...)的ShippingRecipient實現。 我希望在訪問貨件的收件人字段時調用子類方法。 有什么辦法可以代替子類方法嗎? 我知道說結構化屬性是收件人類型會導致調用超類方法,但是也許我不完全理解為什么它們會將子類存儲在receive.class屬性中。
我認為這行不通。 它只會存儲收件人實例。
如果您查看PolyModel的工作方式,那么所有變體都將存儲為基類,在您的示例Recipient
。 它還存儲子類名稱,從數據存儲中檢索它的時間和實體時,它會重新創建特定的子類。
我真的懷疑他們是否會將這種機制構建到StructuredProperty實例化中,並且您發現情況確實如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.