ndb.StructuredProperty不调用ndb.PolyModel子类方法 [英] ndb.StructuredProperty not calling ndb.PolyModel subclass methods
问题描述
在将ndb.Polymodel超类存储为ndb.StructuredProperty时,我无法访问子类方法;超类方法被调用,并引发NotImplementedError。
While storing ndb.Polymodel superclasses as an ndb.StructuredProperty, I was unable to access subclass methods; superclass methods were called instead and raising NotImplementedError's. Here is an abridged version of what I am trying to accomplish.
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存储到了货件的收件人领域。在数据存储中,货件recipient.class == ['Recipient','ShippingRecipient']。当我打电话时:
Now say that I have saved a shipment and stored a ShippingRecipient into the shipment's recipient field. In the datastore, the shipment recipient.class == ['Recipient', 'ShippingRecipient']. When I call:
shipment = Shipment.get_by_id('some_key')
shipment.recipient.PrettyPrinting()
引发NotImplementedError,而不是PrettyPrinting(...)的ShippingRecipient实现。我希望在访问货件的收件人字段时调用子类方法。有没有办法,我可以到达子类方法呢?我知道说结构化属性是Recipient类型会导致超类方法被调用,但是可能我不完全理解他们为什么要将子类存储在recipient.class属性中。
The NotImplementedError is raised instead of the ShippingRecipient implementation of PrettyPrinting(...). I want the subclass method to be called when accessing the shipment's recipient field. Is there a way I can get to the subclass method instead? I know that saying the structured property is of type Recipient causes the superclass method to be called, but then maybe I don't fully understand why they would store the subclass in the recipient.class attribute.
推荐答案
我不相信这可以工作。它只会存储Recipient实例。
如果你看看PolyModel是如何工作的,所有变体都存储为基类,
在你的示例 Recipient
。它还存储子类名称,当它从数据存储中检索时,它会重新创建特定的子类。
I don't believe this can work. It will only store Recipient instances.
If you look at how PolyModel works, all variants are stored as the base class,
in your example Recipient
. It also stores the sub class names, when and entity it is retrieved from the data store, it recreates the specific subclass.
我真的怀疑他们会将此机制构建到StructuredProperty实例化中,并且您已经发现这种情况。
I really doubt they would have built this mechanism into StructuredProperty instantiation, and you have found this to be the case.
这篇关于ndb.StructuredProperty不调用ndb.PolyModel子类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!