MongoEngine GenericReferenceField高级查询

时间:2012-12-16 23:58:42

标签: mongodb mongoengine

概述:

我的用户文档引用带有FileFields的图像文档。您不能使用FileField深度复制对象(为什么不?)。深度复制用户文档取消引用关联的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是两个用户文档。这是事件的样子(顺便说一句,它确实允许继承)。

    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>

1 个答案:

答案 0 :(得分:0)

原来,这是一个错误!我在概述中所说的是正确的。 [错误的状态

有用的链接:

status of the bug

mailing list discussion