1
votes

I'm using Google App Engine for my backend development and I'm using datastore models and Google Cloud Storage to store my image objects. So here's my Media model

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)

And now I also want to upload image thumbnail and store it in the same entity. So what I want is something like

class Media(ndb.Model):
    ...

    thumnail = ndb.LocalStructuredProperty(Media)

But Python doesn't let me to use self class as an argument for class attribute and GAE doesn't allow model names as modelclass argument for ndb.StructuredProperty.

I'm wondering, is there any way to avoid this limitation like lazy initialization or something like that?

1
You could just add the property to the class after the class definition. Media.thumnail = ndb.LocalStructuredProperty(Media) - Tim Hoffman
I don't think that's a good solution, because ndb.Model has a metaclass which does some binding work during model initialization, so I will have to do it by myself. - Kirill Cherepanov

1 Answers

1
votes

You can do something like this:

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)