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

Fix query chaining with .order_by() #176

Merged
merged 1 commit into from Dec 10, 2012

Conversation

pteichman
Copy link
Contributor

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.

This changes order_by() to eliminate its reference to
self._cursor. This meant that any parameters built by QuerySet
that followed an order_by() clause were ignored.
@pteichman
Copy link
Contributor Author

This applies and tests cleanly on 0.6.20 (where we are in production), the 0.7 branch, and master.

rozza added a commit that referenced this pull request Dec 10, 2012
@rozza rozza merged commit 9d52e18 into MongoEngine:master Dec 10, 2012
@rozza
Copy link
Contributor

rozza commented Dec 10, 2012

Thanks for the patch! Fixed in 0.7.8

anemitz pushed a commit to closeio/mongoengine that referenced this pull request Mar 29, 2013
samuelclay added a commit to samuelclay/mongoengine that referenced this pull request Apr 15, 2013
# By Ross Lawley (22) and others
# Via Ross Lawley (8) and zmolodchenko (1)
* 'master' of github.com:samuelclay/mongoengine: (36 commits)
  fix error reporting, where choices is list of flat values
  Removing custom layout
  Adding google analytics
  Update LICENSE
  Update docs/upgrade.rst
  Fixing PY3.3 test cases
  python 3.3 test fixes
  Updated travis python-3.1 no longer supported
  Trying to get travis to build the 0.8 branch
  Updated travis
  0.7.9
  Update AUTHORS
  Corrected user guide link in README
  Version Bump
  Fix sequence fields in embedded documents (MongoEngine#166)
  Update AUTHORS & Changelog (MongoEngine#176)
  Updated Changelog
  Fixed EmailField so can add extra validation
  Updated Changelog
  Add more tests
  ...
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 9d52e18 on pteichman:order-by-chaining into * on MongoEngine:master*.

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

Successfully merging this pull request may close these issues.

None yet

3 participants