-
Notifications
You must be signed in to change notification settings - Fork 429
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
Conversation
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) ```
@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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
@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') |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Other than my comments, looks good! |
Remove argument in tox py.test commands that was added in previous commit
Queryset passes through function to be tinkered with when a column is
sorted.
Similar to
render_FOO
Example Usage: