概述:
我的用户文档引用了带有 FileFields 的图像文档。您不能使用 FileField 对对象进行深度复制(为什么不呢?)。深度复制用户文档会取消引用关联的图像(使用 FileField),因此会失败。
我正在尝试使用 MongoEngine (0.7.8) 查询一个集合,如果我这样查询:
>>> cls.objects(Q(author=devin_user))
[<FollowUserEvent: [<User: Devin> => <User: Strike>]>]
# Querying author works fine
>>> cls.objects(Q(parent=strike_user))
[<FollowUserEvent: [<User: Devin> => <User: Strike>]>]
# Querying parent works fine
>>> cls.objects(Q(parent=strike_user) & Q(author=devin_user))
*** TypeError: 'Collection' object is not callable. If you meant to call the '__deepcopy__' method on a 'Collection' object it is failing because no such method exists.
# Definitely fails here, but why?
# Even stranger, if I combine a query on parent and hidden_at it succeeds, but if I combine a query on author and hidden_at it gloriously fails
>>> cls.objects(Q(parent=strike_user) & Q(hidden_at=None))
[<FollowUserEvent: [<User: Devin> => <User: Strike>]>]
# Querying parent works fine
>>> cls.objects(Q(author=devin_user) & Q(hidden_at=None))
*** TypeError: 'Collection' object is not callable. If you meant to call the '__deepcopy__' method on a 'Collection' object it is failing because no such method exists.
# Boom!
stroke_user 和 devin_user 是两个用户文档。这是 Event 的样子(顺便说一下,它确实允许继承)。
class Event(Document):
"""
:param anti: tells if an event is related to an inverse event
e.g. follow/unfollow, favorite/unfavorite
:param partner: relates an anti-event to an event.
only set on the undoing event
"""
author = GenericReferenceField(required=True)
parent = GenericReferenceField(required=True)
created_at = DateTimeField(required=True, default=datetime.now)
hidden_at = DateTimeField()
anti = BooleanField(default=False)
partner = ReferenceField('Event', dbref=False)
meta = {
'cascade': False,
'allow_inheritance': True }
def __repr__(self):
action = "=/>" if self.anti else "=>"
return "<%s: [%s %s %s]>" % (self.__class__.__name__,
self.author.__repr__(), action, self.parent.__repr__())
对我来说似乎是一个错误,但我很想听到反馈:)
更新:
似乎 mongoengine/queryset.py:98 调用了 copy.deepcopy。这遵循一个 ReferenceField,并尝试复制它的 FileField 数据。不幸的是,这不起作用。
Class Image(Document):
file = FileField(required=True)
Class User(Document):
name = StringField()
image = ReferenceField('Image')
>>> copy.deepcopy(user)
*** TypeError: 'Collection' object is not callable. If ...
>>> user.image = None
>>> copy.deepcopy(user)
<User: Devin>