ndb.StructuredProperty不调用ndb.PolyModel子类方法

2024-06-28 10:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

储存时ndb.多模型超类作为ndb结构属性,我无法访问子类方法;而是调用了超类方法并引发了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存储到装运的recipient字段中。在数据存储中收件人.class==['Recipient','ShippingRecipient']。当我打电话给:

^{pr2}$

引发NotImplementedError,而不是PretyPrinting(…)的ShippingRecipient实现。我希望在访问装运的收件人字段时调用子类方法。有没有一种方法可以代替子类方法?我知道说结构化属性的类型是receiver会导致超类方法被调用,但是也许我不完全理解为什么他们会将子类存储在收件人.class属性。在


Tags: 方法self属性defthis子类收件人class
1条回答
网友
1楼 · 发布于 2024-06-28 10:59:26

我不相信这行得通。它将只存储收件人实例。
如果你看看PolyModel是如何工作的,所有变量都存储为基类, 在您的示例Recipient。它还存储子类的名称,当从数据存储中检索到它的实体时,它会重新创建特定的子类。在

我真的怀疑他们是否会将这种机制构建到StructuredProperty实例化中,您已经发现了这种情况。在

相关问题 更多 >