2012-07-17 59 views
0

定義字段這是我的代碼:使用NDB串

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)   

我需要動態地定義在我的模型領域,但這似乎並沒有工作,因爲字典是不正確的地方放在哪裏我的領域。

對於誰不瞭解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() 

但我仍然得到這個錯誤。

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 972, in _store_value entity._values[self._name] = value TypeError: 'NoneType' object does not support item assignment

這是什麼意思?

回答

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. 
    """ 
+0

這看起來很好,但仍然無法正常工作。剛剛編輯OP的另一個請求。 – Chobeat 2012-07-17 16:46:01

+0

屬性最初設置爲無。嘗試在循環之前分配'self._properties = {}'。 – 2012-07-18 00:47:37