我试图为tinydb制作一个小小的orm但是遇到了类和实例集合的问题:
使用以下代码:
WHERE
这有效:
class SubModelChild(object):
def __init__(self):
print "IN INIT OF SubModelChild"
def cmd2(self):
print "IN cmd2 of SubModelChild"
def search(self):
print "IN search of SubModelChild"
result = []
for x in range(0, 3):
rec = {'id': x, 'text': 'text %d' % x}
result.append(rec)
return result
class SubModelMaster(object):
_child = None
def __init__(self):
print "IN INIT OF SubModelMaster"
self._child = SubModelChild()
def cmd1(self):
print "IN cmd1 of SubModelMaster"
class BaseModel(object):
_store = None
_em = None
def __init__(self):
print "IN INIT OF BaseModel"
self._store = SubModelMaster()
self._em = self._store._child
@classmethod
def objects(cls):
return Manager(cls)
def cmd3(self):
print "IN cmd3 of BaseModel"
class Manager(object):
def __init__(self, model_class):
self.model_class = model_class
self._em = model_class._em
def _model(self, result):
if result is None: return None
instance = self.model_class(**result)
return instance
def find(self):
print "IN find of Manager"
return list(map(self._model, self._em.search()))
class Address(BaseModel):
address = None
city = None
# Works
adr = Address()
adr._store.cmd1()
adr._em.cmd2()
adr.cmd3()
for rec in adr._em.search():
print rec
# Don't works
collection = Address.objects().find()
for rec in collection.find():
print rec
而这不是:
adr = Address()
adr._store.cmd1()
adr._em.cmd2()
adr.cmd3()
for rec in adr._em.search():
print rec
确认此错误:
collection = Address.objects().find()
for rec in collection.find():
print rec
有人可以帮我找到解决方案吗?