Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SortedListField ordering does not work with ReferenceField #503

Open
dzerrenner opened this issue May 17, 2012 · 2 comments
Open

SortedListField ordering does not work with ReferenceField #503

dzerrenner opened this issue May 17, 2012 · 2 comments

Comments

@dzerrenner
Copy link

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
Copy link

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

@nightpool
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants