-
Notifications
You must be signed in to change notification settings - Fork 431
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,75 @@ the ``orderable`` argument:: | |
class SimpleTable(tables.Table): | ||
name = tables.Column() | ||
actions = tables.Column(orderable=False) | ||
|
||
|
||
.. _table.order_foo: | ||
|
||
:meth:`table.order_FOO` methods | ||
-------------------------------- | ||
|
||
Another solution for alternative ordering is being able to chain functions on to | ||
the original queryset. This method allows more complex functionality giving the | ||
ability to use all of Django's QuerySet API. | ||
|
||
Adding a `Table.order_FOO` method (where `FOO` is the name of the column), | ||
gives you the ability to chain to, or modify, the original queryset when that | ||
column is selected to be ordered. | ||
|
||
The method takes two arguments: `queryset`, and `is_descending`. The return | ||
must be a tuple of two elements. The first being the queryset and the second | ||
being a boolean; note that modified queryset will only be used if the boolean is | ||
`True`. | ||
|
||
For example, let's say instead of ordering alphabetically, ordering by | ||
amount of characters in the first_name is desired. | ||
The implementation would look like this: | ||
:: | ||
|
||
# tables.py | ||
from django.db.models.functions import Length | ||
|
||
class PersonTable(tables.Table): | ||
name = tables.Column() | ||
|
||
def order_name(self, queryset, is_descending): | ||
queryset = queryset.annotate( | ||
length=Length('first_name') | ||
).order_by(('-' if is_descending else '') + 'length') | ||
return (queryset, True) | ||
|
||
|
||
|
||
As another example, presume the situation calls for being able to order by a | ||
mathematical expression. In this scenario, the table needs to be able to be | ||
ordered by the sum of both the shirts and the pants. The custom column will | ||
have its value rendered using :ref:`table.render_FOO`. | ||
|
||
This can be achieved like this: | ||
:: | ||
|
||
# models.py | ||
class Person(models.Model): | ||
first_name = models.CharField(max_length=200) | ||
family_name = models.CharField(max_length=200) | ||
shirts = models.IntegerField() | ||
pants = models.IntegerField() | ||
|
||
|
||
# tables.py | ||
from django.db.models import F | ||
|
||
class PersonTable(tables.Table): | ||
clothing = tables.Column() | ||
|
||
class Meta: | ||
model = Person | ||
|
||
def render_clothing(self, record): | ||
return str(record.shirts + record.pants) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: could I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but only if you are ordering it by |
||
).order_by(('-' if is_descending else '') + 'amount') | ||
return (queryset, True) |
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.