Python Google应用程序引擎递归ndb.StructuredProperty
我正在使用Google App Engine进行后端开发,我正在使用数据存储模型和Google云存储来存储我的图像对象。这是我的媒体模型Python Google应用程序引擎递归ndb.StructuredProperty,python,google-app-engine,datastore,Python,Google App Engine,Datastore,我正在使用Google App Engine进行后端开发,我正在使用数据存储模型和Google云存储来存储我的图像对象。这是我的媒体模型 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
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作为class属性的参数,GAE也不允许模型名作为modelclass
参数作为ndb.StructuredProperty
我想知道,有没有办法避免像延迟初始化之类的限制?您可以这样做:
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)
是的,我考虑过这样的解决方案,我希望有一种方法可以做到这一点,而不需要创建单独的模型。我会将您的答案标记为正确答案,如果没有人找到更好的答案,您可以在类定义之后将属性添加到类中
Media.thumnail=ndb.LocalStructuredProperty(Media)
我认为这不是一个好的解决方案,因为ndb.Model有一个元类,它在模型初始化期间执行一些绑定工作,所以我必须自己做。