0
概述:MongoEngine GenericReferenceField高級查詢
我的用戶文檔引用帶有FileFields的Image文檔。您無法使用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!
strike_user和devin_user兩種用戶的文檔。這是事件的樣子(順便說一下,它允許繼承)。
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個來電覆制.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>
我真的相信這是一個錯誤。我已經解決了這個問題,通過在子類和(現在是抽象的)父類的GenericReferenceFields上將'author'和'parent'定義爲'ReferenceField's(dbref = True)。 // 現在當我查詢時,我必須在子類上這樣做,即使我已經在父級上將'author'和'parent'保留爲'GenericReferenceField'。奇怪的是,即使我在孩子的「author」和「parent」字段中指定了dbref = True,我無法查詢父類:即: >>> docs.Event.objects(author = docs.Event.objects [ 0] .author)#[] – Devin
https://groups.google.com/forum/?fromgroups=#!searchin/mongoengine-users/GenericReferenceField/mongoengine-users/4S3ITUE7K5s/vEuISza3XEwJ更多關於此,順便說一句。 – Devin