Reputation: 185
Is it possible to assign a dynamic Entity Kind to an Expando Model? For example, I want to use this model for many types of dynamic entities:
class Dynamic(ndb.Expando):
"""
Handles all "Post types", such as Pages, Posts, Users, Products, etc...
"""
col = ndb.StringProperty()
parent = ndb.IntegerProperty()
name = ndb.StringProperty()
slug = ndb.StringProperty()
Right now I use the "col" StringProperty
to hold the Kind (like "Pages", "Posts", etc) and query for the "col" every time.
After reading the docs, I stumbled upon this @classmethod:
class MyModel(ndb.Model):
@classmethod
def _get_kind(cls):
return 'AnotherKind'
Does that mean I can do this?
class Dynamic(ndb.Expando):
"""
Handles all "Post types", such as Pages, Posts, Users, Products, etc...
"""
col = ndb.StringProperty()
parent = ndb.IntegerProperty()
name = ndb.StringProperty()
slug = ndb.StringProperty()
@classmethod
def _get_kind(cls):
return 'AnotherKind'
But how do I dynamically replace 'AnotherKind'? Can I do something like return col
?
Thanks!
Upvotes: 0
Views: 208
Reputation: 16563
I don't know if you can do that, but it sounds dangerous, and GAE updates might break your code.
Using subclasses seems like a much safer alternative. Something like this:
class Dynamic(ndb.Expando):
parent = ndb.IntegerProperty()
name = ndb.StringProperty()
slug = ndb.StringProperty()
class Pages(Dynamic):
pass
class Posts(Dynamic):
pass
class Users(Dynamic):
pass
You could also try using PolyModel
.
We need to know more about your application and what you are trying to accomplish to give more specific advice.
Upvotes: 1