这些是我正在导入的模块
from google.appengine.ext import ndb
from google.appengine.ext import db
这是我的代码使用Model(并收到错误)的地方
#class_checking = Class_checking(id = classname) --> how i created Object
#......#
#class_c_prereq is the key_name that was made manually
class_c_update = Model.get_by_key_name(class_c_prereq)
#I need to retrieve the entity, but Model is undefined
这是追溯错误
File "C:\Users\Desktop\classes\main.py", line 59, in post
self.updating(class_data)
File "C:\Users\Desktop\classes\main.py", line 40, in updating
class_c_update =Model.get_by_key_name(class_c_prereq)
NameError: global name 'Model' is not defined
通过以下方式创建Class_checking实体:
class_checking = Class_checking(classC_name = classname, classC_prereq = prereq, classC_point_To = 0, classC_point_By = 1, classC_array = class_data_JSON, id = classname )
然后我有一个单独的函数,用于更新属性classC_point_To
class_data = Class_checking.query().fetch() #gets all entities of Class
for class_c1 in class_data:
class_c_prereq = class_c1.classC_prereq #gets the class prereq, only 1 prereq
for prereq_class in class_data: #loop through all entities of Class
if prereq_class.classC_name == class_c_prereq: #if the class name == prereq
class_c_update = db.Model.get_by_key_name(class_c_prereq) #update the class name so that it "points to" a class
class_c_update.classC_point_To = 1
class_c_update.put()
答案 0 :(得分:1)
您必须使用模型子类按键获取数据。在您的示例中,您的模型为Class_checking
,因此Class_checking.get_by_key_name(class_c_prereq)
子类的正确类方法db.Model
和Class_checking.get_by_id(class_c_prereq)
子类的ndb.Model
。
答案 1 :(得分:0)
在这种情况下你需要引用db。
使用:
class_c_update = db.Model.get_by_key_name(...)
或
from google.appengine.ext.db import Model