2016-12-16 106 views
1

我尝试继承财产NDB如下面的例子是否可以从属性中获取模型名称和字段名称?

class FileStorageProperty(ndb.StringProperty): 

    def __init__(self, **kwds): 
     super(FileStorageProperty, self).__init__(**kwds) 

    def _to_base_type(self, value): 
     # Do somethings to get field name and model name 
     return None 

    def _from_base_type(self, value): 
     return None 

这样的方式使用FileStorageProperty是出头像

class A(ndb.Model): 
    file = FileStorageProperty() 

我希望做的是,FileStorageProperty类如内。 init,_to_base_type,_from_base_type我想知道模型名称('A')和字段名称('文件')。

谢谢你的任何建议。

+0

FileStorage Class在哪里?在你的例子中只有FileStorageProperty类,不清楚你想要什么:) – linpingta

+0

为我的错误道歉,我的意思是在FileStorageProperty类中。我只是编辑问题以便更好地理解。 – Chetchaiyan

+0

您不能..因为您正在为另一个类A内的FileStorageProperty类创建对象,那么如何让A类在FileStorageProperty类中可用。双向的方式是不可能的。 –

回答

0

做到这一点的唯一方法是将信息作为参数传递。您可能必须将该类的名称和该字段的名称作为字符串传递,因为我怀疑该类在创建该字段时不存在。

class FileStorageProperty(ndb.StringProperty): 

    def __init__(self, model, field, **kwds): 
     self._model = model 
     self._field = field 
     super(FileStorageProperty, self).__init__(**kwds) 

class A(ndb.Model): 
    file = FileStorageProperty("A", "file")