0

这是我的代码:

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()

但我仍然收到此错误。

_store_value entity._values[self._name] = value TypeError: 'NoneType' object 中的文件“C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py”,第 972 行不支持项目分配

这是什么意思?

4

2 回答 2

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.
  """
于 2012-07-17T16:25:12.687 回答
0

对具有动态属性的模型使用expando 模型

于 2012-07-17T17:48:18.933 回答