Ticket #17252: r17104-admin-order-field.diff

File r17104-admin-order-field.diff, 3.5 KB (added by Sebastian Goll, 13 years ago)
  • django/contrib/admin/views/main.py

     
    169169            ordering = self.lookup_opts.ordering
    170170        return ordering
    171171
     172    def _get_admin_order_field(self, field_name):
     173        """
     174        Get the name of the admin order field defined for the given
     175        element of 'list_display'. The given object can either be the
     176        name of a proper field, the name of a method (on the admin or
     177        model) with the 'admin_order_field' attribute, or a callable
     178        with that attribute. If no admin order is defined for the
     179        given object, the method returns None.
     180        """
     181        try:
     182            f = self.lookup_opts.get_field(field_name)
     183        except models.FieldDoesNotExist:
     184            # See whether field_name is a name of a non-field
     185            # that allows sorting.
     186            try:
     187                if callable(field_name):
     188                    attr = field_name
     189                elif hasattr(self.model_admin, field_name):
     190                    attr = getattr(self.model_admin, field_name)
     191                else:
     192                    attr = getattr(self.model, field_name)
     193                return attr.admin_order_field
     194            except AttributeError:
     195                return None
     196        else:
     197            return f.name
     198
    172199    def get_ordering(self, request):
    173200        params = self.params
    174201        # For ordering, first check the if exists the "get_ordering" method
     
    185212                try:
    186213                    none, pfx, idx = p.rpartition('-')
    187214                    field_name = self.list_display[int(idx)]
    188                     try:
    189                         f = self.lookup_opts.get_field(field_name)
    190                     except models.FieldDoesNotExist:
    191                         # See whether field_name is a name of a non-field
    192                         # that allows sorting.
    193                         try:
    194                             if callable(field_name):
    195                                 attr = field_name
    196                             elif hasattr(self.model_admin, field_name):
    197                                 attr = getattr(self.model_admin, field_name)
    198                             else:
    199                                 attr = getattr(self.model, field_name)
    200                             field_name = attr.admin_order_field
    201                         except AttributeError:
    202                             continue # No 'admin_order_field', skip it
    203                     else:
    204                         field_name = f.name
     215                    order_field = self._get_admin_order_field(field_name)
     216                    if not order_field:
     217                        continue # No 'admin_order_field', skip it
    205218
    206                     ordering.append(pfx + field_name)
     219                    ordering.append(pfx + order_field)
    207220
    208221                except (IndexError, ValueError):
    209222                    continue # Invalid ordering specified, skip it.
     
    235248                    # No match, but there might be a match if we take into
    236249                    # account 'admin_order_field'
    237250                    for _index, attr in enumerate(self.list_display):
    238                         if getattr(attr, 'admin_order_field', '') == field:
     251                        if self._get_admin_order_field(attr) == field:
    239252                            index = _index
    240253                            break
    241254                if index is not None:
Back to Top