| 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 | |
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 |