hmarr / mongoengine

[Moved to mongoengine/mongoengine]
https://github.com/MongoEngine/mongoengine
MIT License
795 stars 20 forks source link

SortedListField ordering does not work with ReferenceField #503

Open dzerrenner opened 12 years ago

dzerrenner commented 12 years ago

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()
idelgado commented 11 years ago

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

nightpool commented 10 years ago

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)