Google App Engine 递归 ndb.StructuredProperty

Google App Engine recursive ndb.StructuredProperty

我正在使用 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 = 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)