我使用 Google App Engine 进行后端开发,并使用数据存储模型和 Google Cloud Storage 来存储图像对象。这是我的媒体模型
class Media(ndb.Model):
url = ndb.StringProperty(indexed=False) # url generated by images.get_serving_url
path = ndb.StringProperty(indexed=False) # path in GCP
width = ndb.IntegerProperty(indexed=False)
height = ndb.IntegerProperty(indexed=False)
size = ndb.IntegerProperty()
created = ndb.DateTimeProperty(auto_now_add=True)
现在我还想上传图像缩略图并将其存储在同一个实体中。所以我想要的是这样的
class Media(ndb.Model):
...
thumnail = ndb.LocalStructuredProperty(Media)
但是 Python 不允许我使用 self class 作为类属性的参数,并且 GAE 不允许模型名称作为 ndb.StructuredProperty
的 modelclass
参数。
我想知道,有什么方法可以避免这种限制,例如延迟初始化或类似的东西?
最佳答案
你可以这样做:
class Media(ndb.Model):
url = ndb.StringProperty(indexed=False)
path = ndb.StringProperty(indexed=False)
width = ndb.IntegerProperty(indexed=False)
height = ndb.IntegerProperty(indexed=False)
size = ndb.IntegerProperty()
created = ndb.DateTimeProperty(auto_now_add=True)
class Thumbnail(Media):
pass
class FullSize(Media):
thumbnail = ndb.LocalStructuredProperty(Thumbnail)
关于python - Google App Engine 递归 ndb.StructuredProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543216/