MongoEngine/mongoengine — pull request #176 by pteichman (2012-11-27 16:08:19+00:00)
Title: Fix query chaining with .order_by()

We create helper methods in our models for common filtering operations. Here's an example:

@classmethod
def filter_by_user(cls, user, order_by="-ctime", include_hidden=False, **kwargs):
params = {"user_id": user.id}

if not include_hidden:
params["hidden__ne"] = True

params.update(kwargs)

return cls.objects(**params).order_by(order_by)

This gives us a general-purpose filtering function that we can use elsewhere:

Foo.filter_by_user(user).only("id")

However, the implementation of .order_by() accesses QuerySet._cursor, which effectively freezes the query. An .only() clause doesn't take effect at all.

This patch delays the effect of .order_by() until the QuerySet is executed.