python - Google App Engine 递归 ndb.StructuredProperty

标签 python google-app-engine datastore

我使用 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.StructuredPropertymodelclass 参数。

我想知道,有什么方法可以避免这种限制,例如延迟初始化或类似的东西?

最佳答案

你可以这样做:

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/

相关文章:

python - 操作错误(2003、 "Can' t 连接到 '127.3.138.130' (111) 上的 MySQL 服务器")

google-app-engine - GAE : Resource interpreted as Stylesheet but transferred with MIME type text/html

java - AppEngine 开发服务器的数据查看器

templates - Ext.form.ComboBox : Use template for displayField

azure - Azure ML 工作区中的数据 Assets

javascript - 如何用python获取webbrowser的源码

python - 保留文件描述符以防止 FD 耗尽

python - tkFileDialog.askopenfilename如何指定其他驱动器?

javax.naming.initialcontext 是一个受限制的类 google app engine

android - 无法根据链接接收对象