-
Notifications
You must be signed in to change notification settings - Fork 423
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
Allow column to be ordered by ascending by default #566
Conversation
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.
Hi @karech, interesting use case, thanks for the pull!
Just to be sure, doesn't using order_by='-name'
give the desired result?
Anyway, we need to have this properly documented (at least in the Column
docstring and tested before it's acceptable.
django_tables2/columns/base.py
Outdated
if self.column.default_descending_ordering: | ||
order_by.next = order_by if self.is_ordered else order_by.opposite | ||
else: | ||
order_by.next = order_by.opposite if self.is_ordered else order_by |
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.
what about:
order_by.next = order_by.opposite if self.is_ordered else order_by
if self.column.default_descending_ordering:
order_by.next = order_by.opposite
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.
Nope, I think this will break.
@@ -111,7 +113,7 @@ class Column(object): | |||
def __init__(self, verbose_name=None, accessor=None, default=None, | |||
visible=True, orderable=None, attrs=None, order_by=None, | |||
empty_values=None, localize=None, footer=None, | |||
exclude_from_export=False): | |||
exclude_from_export=False, default_descending_ordering=False): |
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.
Naming is certainly a good question.
default_ordering_desc
?
default_ordering='-'
for descending?
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'm not sure about "default", because this only affects BoundColumn.order_by_alias.next
.
Maybe first_ordering_descending=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.
I'd say initial
then.
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.
initial_descending_ordering
?
@jieter Let me try to explain, what I wanna achieve. I have tables, and custom columns (lets say NumberColumn, DecimalColumn(NunberColumn) and etc). And I wanna them always be sorted by descending on first user click. class NumberColumn(django_tables2.Column):
def __init__(*args, **kwargs):
super().__init__(*args, **kwargs)
self.default_descending_ordering = True And on first click on column they (columns with numbers) will be sorted by descending, but other columns (like text columns) - by ascending. |
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.
@karech implementation looks good now, but before I'm going to merge this, it needs to have a good test. Especially with these less commonly used features, it's easy for a future contributor to break it accidentally if no tests cover it.
Let me know if you need any help.
@@ -96,6 +96,8 @@ class Column(object): | |||
- If `True`, force localization | |||
- If `False`, values are not localized | |||
- If `None` (default), localization depends on the ``USE_L10N`` setting. | |||
default_descending_ordering (bool): If 'True', | |||
column.order_by_alias.next will return descending ordering if not ordered. |
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.
Can you add to this sentence that it defaults to False
?
@jieter Hello! I'v added tests (tried to make same styling as other tests). And I'm still not sure about naming of parameter. Looks not very clear. |
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.
Hi @karech nice work. I added a comment about the style of test.
It would also be nice to actually assert that data is ordered descending initially.
table_desc = TableDescOrd(data) | ||
|
||
assert table.columns[1].order_by_alias.next == 'field' | ||
assert table_desc.columns[1].order_by_alias.next == '-field' |
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.
Can use a TestCase-derived test class? Using its assertion methods (like self.assertEqual()
) results in much more informative failed assertion messages.
Closing this after merging #597 |
Not sure about: