Python MongoAlchemy StringField意外被QueryField替换?

Python MongoAlchemy StringField意外被QueryField替换?,python,flask,flask-extensions,mongoalchemy,Python,Flask,Flask Extensions,Mongoalchemy,在与MongoAlchemy合作时,我一直在与一个奇怪的bug作斗争 我的参与者模型上有一个name=db.StringField()。大多数情况下,它工作正常,但由于某些原因,该字段偶尔会被QueryField覆盖。如果我从模块中重新加载我的参与者类,它会正常工作一段时间,但随后再次中断 以下是发生这种情况的终端会话: In [99]: from webapp.models import * In [100]: Participant.by_name('yaronTesting') # My

在与MongoAlchemy合作时,我一直在与一个奇怪的bug作斗争

我的
参与者
模型上有一个
name=db.StringField()
。大多数情况下,它工作正常,但由于某些原因,该字段偶尔会被
QueryField
覆盖。如果我从模块中重新加载我的
参与者
类,它会正常工作一段时间,但随后再次中断

以下是发生这种情况的终端会话:

In [99]: from webapp.models import *

In [100]: Participant.by_name('yaronTesting') # My own method, which filters by name
Out[100]: <Participant yaronTesting>

In [101]: Participant.query.all()
Out[101]: [<Participant name>, <Participant name>]

# What?

In [102]: Participant.query.all()[0]
Out[102]: <Participant name>

In [103]: Participant.query.all()[0].name
Out[103]: QueryField(name)

In [104]: from webapp.models import Participant

In [105]: Participant.query.all()
Out[105]: [<Participant yaronTesting>, <Participant tsviki>]

In [106]: Participant.query.all()[0].name
Out[106]: u'yaronTesting'
最后,当我将
name
字段添加到
\uuuu init\uuuu
函数并尝试实例化一些对象时,出现了一个错误(早期版本没有向init传递任何参数):

[36]中的
:Participant.query.all()
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 Participant.query.all()
/用户/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/query.pyc全部(self)
177 def all(自身):
178“”返回列表“”中查询的所有结果
-->179返回[iter中obj的obj(自身)]
180
181 def distinct(自身,钥匙):
/用户/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/query.pyc在next(self)中
410
411 def next(自我):
-->412返回自我。\u下一个\u内部()
413下一个下一个
414
/用户/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/query.pyc in\u next\u internal(self)
421如果obj:
422返回obj
-->423 value=self.session.\u展开(self.type,value,fields=self.fields)
424如果不存在(值,dict):
425 self.session.cache\u write(值)
/用户/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/session.pyc in_unwrap(self、type、obj、**kwargs)
333 def_展开(自身、类型、obj、**kwargs):
334 obj=类型转换\传入(obj,会话=自身)
-->335返回类型.展开(obj,session=self,**kwargs)
336
337@property
/展开中的用户/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/document.pyc(cls、obj、字段、会话)
494如果字段不是无:
495参数['retrieved_fields']=字段
-->496 obj=cls(从_db=True加载,**参数)
497 obj._mark_clean()
498 obj.\u会话=会话
/用户/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/webapp/webapp/models.py in_uuuuuuuinit_uuuuuu(self,name,**kwargs)
319
320 def_uuuinit_uuu(self,name,**kwargs):
-->321超级(参与者,自我).\u初始__(
322 name=名称,
323**夸尔格)
TypeError:super(type,obj):obj必须是类型的实例或子类型

这里是MongoAlchemy的作者。我不太熟悉flask集成,因为它是别人写的,但这里必须发生的是all()返回的是类参与者的实例,而不是从数据库数据创建的实例


如果你能给出一个完整的可运行的例子,我可以给你更多的细节,但这就是我从你的问题中所能收集到的所有信息。

嘿,杰夫,谢谢你的提示。我意识到我没有给出尽可能有用的例子——我仍然没有成功地复制行为。我会坚持下去,然后再来一些。还有,很酷的图书馆,谢谢。
In [168]: tsviki_old
Out[168]: <Participant name>

In [169]: tsviki_old.mongo_id
Out[169]: ObjectId('53f4f27a6c4dae23aba41419')

In [170]: tsviki_new = Participant.by_name('tsviki')
Out[170]: <Participant tsviki>

In [171]: tsviki_new.mongo_id
Out[171]: ObjectId('53f4f27a6c4dae23aba41419')

In [172]: tsviki_old.mongo_id
Out[172]: ObjectId('53f4f27a6c4dae23aba41419')

In [173]: tsviki_old
Out[173]: <Participant name>

In [174]: tsviki_new
Out[174]: <Participant tsviki>

In [175]: tsviki_old.name
Out[175]: QueryField(name)

In [176]: tsviki_new.name
Out[176]: u'tsviki'

In [179]: tsviki_old.created
Out[179]: QueryField(created)

In [180]: tsviki_old.modified
Out[180]: QueryField(modified)

In [181]: tsviki_old.sessions
Out[181]: QueryField(sessions)

# It happened again! To tsviki_new!

In [182]: tsviki_new.created
Out[182]: QueryField(created)

In [183]: tsviki_new
Out[183]: <Participant name>

In [184]: tsviki_new.name
Out[184]: QueryField(name)

In [185]: tsviki_new.mongo_id
Out[185]: ObjectId('53f4f27a6c4dae23aba41419')
@classmethod
def by_name(cls, name):
    return Participant.query.filter({'name': name}).first()
In [36]: Participant.query.all()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-310f11265a76> in <module>()
----> 1 Participant.query.all()

/Users/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/query.pyc in all(self)
    177     def all(self):
    178         ''' Return all of the results of a query in a list'''
--> 179         return [obj for obj in iter(self)]
    180 
    181     def distinct(self, key):

/Users/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/query.pyc in next(self)
    410 
    411     def next(self):
--> 412         return self._next_internal()
    413     __next__ = next
    414 

/Users/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/query.pyc in _next_internal(self)
    421             if obj:
    422                 return obj
--> 423             value = self.session._unwrap(self.type, value, fields=self.fields)
    424             if not isinstance(value, dict):
    425                 self.session.cache_write(value)

/Users/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/session.pyc in _unwrap(self, type, obj, **kwargs)
    333     def _unwrap(self, type, obj, **kwargs):
    334         obj = type.transform_incoming(obj, session=self)
--> 335         return type.unwrap(obj, session=self, **kwargs)
    336 
    337     @property

/Users/kronosapiens/Dropbox/Documents/Development/code/environments/pm/lib/python2.7/site-packages/mongoalchemy/document.pyc in unwrap(cls, obj, fields, session)
    494         if fields is not None:
    495             params['retrieved_fields'] = fields
--> 496         obj = cls(loading_from_db=True, **params)
    497         obj._mark_clean()
    498         obj._session = session

/Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/webapp/webapp/models.py in __init__(self, name, **kwargs)
    319 
    320     def __init__(self, name, **kwargs):
--> 321         super(Participant, self).__init__(
    322             name=name,
    323             **kwargs)

TypeError: super(type, obj): obj must be an instance or subtype of type