개요 :MongoEngine GenericReferenceField 고급 쿼리
내 사용자 문서는 FileFields를 사용하여 이미지 문서를 참조합니다. FileField로 오브젝트를 deepcopy 할 수는 없습니다 (왜 안 되니?). 사용자 문서를 크게 복사하면 관련된 Image (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 두 사용자 문서입니다. 다음은 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 개 통화 복사 .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>
은 정말이 버그 믿는다. 나는 자식 클래스와 부모 클래스의'GenericReferenceField'에 비해'author'와'parent'를'ReferenceField' (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/mongoengineusers/4S3ITUE7K5s/vEuISza3XEwJ 조금 더 자세히 알아보십시오. – Devin