这是我的代码:
class SocialNodeSubscription(model.Model):
def __init__(self, *args, **kwargs):
permissions=["post","read","reply","admin"]
for p in permissions:
self.__dict__["can_"+p]=model.BooleanProperty(default=True)
我需要动态定义模型中的字段,但这似乎不起作用,因为 dict 不是放置字段的正确位置。
对于谁不了解ndb,这就是看起来更简单的方式。
class SocialNodeSubscription(model.Model):
def __init__(self, *args, **kwargs):
self.can_write=model.BooleanProperty(default=True)
self.can_read=model.BooleanProperty(default=True)
...
编辑:
现在我的代码看起来像这样:
def __init__(self, *args, **kwargs):
permissions=["post","read","reply","admin"]
for p in permissions:
self._properties["can_"+p]=model.BooleanProperty(default=True)
self._fix_up_properties()
但我仍然得到这个错误。
文件“C:\ Program Files (x86)\ Google \ google_appengine \ google \ appengine \ ext \ ndb \ model.py“,行 972,在_store_value中 entity._values [self._name] = value TypeError:'NoneType'对象不支持项目分配
这是什么意思?
答案 0 :(得分:1)
这是_properties, 只需看看它的元类MetaModel和类方法_fix_up_properties。
_properties的定义:
# Class variables updated by _fix_up_properties()
_properties = None
方法:
@classmethod
def _fix_up_properties(cls):
"""Fix up the properties by calling their _fix_up() method.
Note: This is called by MetaModel, but may also be called manually
after dynamically updating a model class.
"""
答案 1 :(得分:0)
对具有动态属性的模型使用expando model。