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

Column ordering queryset passthrough #330

Merged
merged 4 commits into from
May 23, 2016

Conversation

michaelmob
Copy link
Contributor

@michaelmob michaelmob commented May 18, 2016

Queryset passes through function to be tinkered with when a column is
sorted.

Similar to render_FOO

Example Usage:

def order_success_rate(self, queryset, is_descending):
    # Custom queryset ordering that uses the database to calculate the
    # success rate of a webpage game
    queryset = queryset.extra(
        select={'value': '(clicks / completed) * 100'},
        order_by=(('-' if is_descending else '') + 'value',)
    )

    # Tuple of reordered queryset and a boolean marking as reordered so it will
    # override the order of the column
    return (queryset, True)

Queryset passes through function to be tinkered with when a column is
sorted.

Similar to `render_FOO`

Example Usage:
```python
def order_success_rate(self, queryset, is_descending):
    # Custom queryset ordering that uses the database to calculate the
    # success rate of a webpage game
    queryset = queryset.extra(
        select={'value': '(clicks / completed) * 100'},
        order_by=(('-' if is_descending else '') + 'value',)
    )

    # Tuple of reordered queryset and a boolean marking as reordered so
it will
    # override the order of the column
    return (queryset, True)
```
@jieter
Copy link
Owner

jieter commented May 18, 2016

@thetarkus thanks for splitting it up. This PR makes one test fail. It would be nice if you could also add a test for this new functionality.

"""
Returns the queryset of the table.

This method can be overridden by :ref:`table.order_FOO` methods on the
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this reference point to anything?

Copy link
Contributor Author

@michaelmob michaelmob May 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

table.order_FOO is a placeholder name, so no, it doesn't point to a real function. Would you like me to rephrase it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say to either add some documentation or do not reference it.

Of course, more documentation is better.

def order_first_name(self, queryset, is_descending):
# annotate to order by length of first_name + last_name
queryset = queryset.annotate(
length=Length("first_name") + Length("last_name")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use single quotes if possible.

@jieter
Copy link
Owner

jieter commented May 19, 2016

@thetarkus looking good, thanks for adding the tests. Would be really nice if you could also add some documentation for this feature in docs/


def order_clothing(self, queryset, is_descending):
queryset = queryset.annotate(
amount=F('shirts') + F('pants')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: could I use record.amount in render_clothing now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but only if you are ordering it by clothing. If the user specifies a different column's order then that attribute won't be there.

@jieter
Copy link
Owner

jieter commented May 20, 2016

Other than my comments, looks good!

Remove argument in tox py.test commands that was added in previous
commit
@jieter jieter merged commit 8b49baa into jieter:master May 23, 2016
jieter added a commit that referenced this pull request May 23, 2016
@michaelmob michaelmob deleted the column-ordering branch May 24, 2016 05:21
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.

3 participants