mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

SortedListField ordering does not work with ReferenceField

Open dzerrenner opened this issue 12 years ago • 2 comments

Changing the tests like shown below fails with error

File "[...]/tests/fields.py", line 38, in test_sorted_list_sorting post.save() File "[...]\document.py", line 183, in save doc = self.to_mongo() File "[...]\base.py", line 956, in to_mongo data[field.db_field] = field.to_mongo(value) File "[...]\fields.py", line 538, in to_mongo return sorted(value, key=itemgetter(self._ordering), reverse=self._order_reverse) TypeError: 'DBRef' object is not subscriptable

def test_sorted_list_sorting(self):
    """Ensure that a sorted list field properly sorts values.
    """
    class Comment(Document):
        order = IntField()
        content = StringField()

    class BlogPost(Document):
        content = StringField()
        comments = SortedListField(ReferenceField(Comment),
            ordering='order')
        tags = SortedListField(StringField())

    post = BlogPost(content='Went for a walk today...')
    post.save()

    post.tags = ['leisure', 'fun']
    post.save()
    post.reload()
    self.assertEqual(post.tags, ['fun', 'leisure'])

    comment1 = Comment(content='Good for you', order=1)
    comment1.save()
    comment2 = Comment(content='Yay.', order=0)
    comment2.save()
    comments = [comment1, comment2]
    post.comments = comments
    post.save()
    post.reload()
    self.assertEqual(post.comments[0].content, comment2.content)
    self.assertEqual(post.comments[1].content, comment1.content)

    BlogPost.drop_collection()

dzerrenner avatar May 17 '12 16:05 dzerrenner

Still seems to be the case from what I can tell in mongoengine 0.8.2

idelgado avatar Jul 19 '13 02:07 idelgado

There's also no way to define a custom sorting order, as far as I can tell. (Which is at the root of this problem)

nightpool avatar Jun 02 '14 13:06 nightpool