Skip to content

Preserve ordering of allowed_sort_fields

decentral1se requested to merge tooreht:feature/ordered_sort_fields into master

Created by: tooreht

Hi,

I like your SortableListView but I missed one thing: The order of allowed_sort_fields is not preserved! This can be very useful when e.g. creating column based sorting in a table, because I don't have to hardcode the order in my template. With my changes one can define the ordering in allowed_sort_fields when using e.g. a tuple of tuples similar to defining fieldsets in the django ModelAdmin:

default_sort_field = 'id'
allowed_sort_fields = (
    (default_sort_field, {'default_direction': '-', 'verbose_name': 'ID'}),
    ('vin', {'default_direction': '', 'verbose_name': 'VIN'}),
    ('sn', {'default_direction': '', 'verbose_name': _('Serial Number')}),
    ('customer', {'default_direction': '', 'verbose_name': _('Customer')}),
    ('customer__postal_code', {'default_direction': '', 'verbose_name': _('Postal Code')}),
)

For backwards compatibility the current dict approach works nevertheless.

Merge request reports