Login

Allow multiple field sorting in a single admin table column

Author:
matiascelasco
Posted:
March 19, 2015
Language:
Python
Version:
1.6
Score:
1 (after 1 ratings)

This snippets extends ModelAdmin in order to allow multi field sorting in admin tables.

Usage example:

class MyModelAdmin(MultiFieldSortableModelAdmin):

    list_display = (
        ...
        'user_full_name',
        ...
    )

    def user_full_name(self, obj):
        return obj.user.get_full_name()
    user_full_name.admin_order_field = ['user__first_name', 'user__last_name']`
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from django.contrib import admin

ORDER_VAR = admin.views.main.ChangeList.ORDER_VAR


class MultiFieldSortableChangeList(admin.views.main.ChangeList):
    """
    This class overrides the behavior of column sorting in django admin tables in order
    to allow multi field sorting


    Usage:

    class MyCustomAdmin(admin.ModelAdmin):

        ...

        def get_changelist(self, request, **kwargs):
            return MultiFieldSortableChangeList

        ...

    """

    def get_ordering(self, request, queryset):
        """
        Returns the list of ordering fields for the change list.
        First we check the get_ordering() method in model admin, then we check
        the object's default ordering. Then, any manually-specified ordering
        from the query string overrides anything. Finally, a deterministic
        order is guaranteed by ensuring the primary key is used as the last
        ordering field.
        """
        params = self.params
        ordering = list(self.model_admin.get_ordering(request)
                        or self._get_default_ordering())
        if ORDER_VAR in params:
            # Clear ordering and used params
            ordering = []
            order_params = params[ORDER_VAR].split('.')
            for p in order_params:
                try:
                    none, pfx, idx = p.rpartition('-')
                    field_name = self.list_display[int(idx)]

                    # the following 8 lines are the only ones modified by me------------------------
                    order_fields = self.get_ordering_field(field_name)
                    # I ask for __iter__ because hasattr(x, '__iter__') is true for list and tuples
                    # but false for strings
                    # http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable
                    if not hasattr(order_fields, '__iter__'):
                        order_fields = [order_fields]
                    for order_field in order_fields:
                        if order_field:
                            ordering.append(pfx + order_field)
                except (IndexError, ValueError):
                    continue  # Invalid ordering specified, skip it.

        # Add the given query's ordering fields, if any.
        ordering.extend(queryset.query.order_by)

        # Ensure that the primary key is systematically present in the list of
        # ordering fields so we can guarantee a deterministic order across all
        # database backends.
        pk_name = self.lookup_opts.pk.name
        if not (set(ordering) & set(['pk', '-pk', pk_name, '-' + pk_name])):
            # The two sets do not intersect, meaning the pk isn't present. So
            # we add it.
            ordering.append('-pk')

        return ordering


class MultiFieldSortableModelAdmin(admin.ModelAdmin):
    """
    By inherit from this class, now is possible to define admin_order_field like this:

    def user_full_name(self, obj):
        return obj.get_full_name()
    user_full_name.admin_order_field = ['first_name', 'last_name']

    """

    def get_changelist(self, request, **kwargs):
        return MultiFieldSortableChangeList

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.