Python 是否可以从属性中获取模型名和字段名?
我尝试将ndb属性子类化,如下例所示Python 是否可以从属性中获取模型名和字段名?,python,google-app-engine,Python,Google App Engine,我尝试将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
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
因此,使用FileStorage属性的方法如下
class A(ndb.Model):
file = FileStorageProperty()
我想做的是,在FileStorageProperty类中,例如init、\u到\u base\u类型、\u从\u base\u类型,我想知道模型名('A')和字段名('file')
谢谢您的建议。唯一的方法是将信息作为参数传递。您可能必须将类的名称和字段的名称作为字符串传递,因为我怀疑在创建字段时该类不存在
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")
唯一的方法是将信息作为参数传递。您可能必须将类的名称和字段的名称作为字符串传递,因为我怀疑在创建字段时该类不存在
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")
文件存储类在哪里?在您的示例中只有FileStorageProperty类,不清楚您想要什么:)为我的错误道歉,我的意思是在FileStorageProperty类内部。我只是为了更好的理解而修改这个问题。你不能。。既然您是在另一个类A中为
FileStorageProperty
类创建对象,那么类A如何在FileStorageProperty类中可用呢。双向方式不可能。文件存储类在哪里?在您的示例中只有FileStorageProperty类,不清楚您想要什么:)为我的错误道歉,我的意思是在FileStorageProperty类内部。我只是为了更好的理解而修改这个问题。你不能。。既然您是在另一个类A中为FileStorageProperty
类创建对象,那么类A如何在FileStorageProperty类中可用呢。双向的方式是不可能的。