Skip to main content
 首页 » 编程设计

python之具有动态 TextProperty 的 NDB Expando 模型

2025年05月04日12lyhabc

我正在尝试:

MyModel({'text': db.Text('text longer than 500 byets')}) 

但是得到:

BadValueError: Indexed value fb_education must be at most 500 bytes 

我认为这只是旧数据库 API 的这个问题的延续。

https://groups.google.com/forum/?fromgroups#!topic/google-appengine/wLAwrjtsuks

请您参考如下方法:

首先动态创建实体:

 kindOfEntity = "MyTable" 
class DynamicEntity(ndb.Expando): 
     @classmethod 
     def _get_kind(cls): 
        return kindOfEntity 

然后在运行时/动态分配文本属性,如下所示

dbObject = DynamicEntity() 
key = "studentName" 
value = "Vijay Kumbhani" 
textProperties = ndb.TextProperty(key) 
dbObject._properties[key] = {} 
dbObject._values[key] = {} 
dbObject._properties[key] = textProperties  
dbObject._values[key] = value 
dbObject.put() 

然后在键属性分配给文本属性之后