Ticket #14977: admin_multi_ordering.patch
File admin_multi_ordering.patch, 5.0 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/templatetags/admin_list.py
110 110 111 111 th_classes = [] 112 112 new_order_type = 'asc' 113 if field_name == cl.order_field or admin_order_field == cl.order_field:114 th_classes.append('sorted %sending' % cl.order_type .lower())115 new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type .lower()]113 if field_name == cl.order_field[0] or admin_order_field == cl.order_field[0]: 114 th_classes.append('sorted %sending' % cl.order_type[0].lower()) 115 new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type[0].lower()] 116 116 117 117 yield { 118 118 "text": header, -
django/contrib/admin/views/main.py
130 130 131 131 def get_ordering(self): 132 132 lookup_opts, params = self.lookup_opts, self.params 133 # For ordering, first check the "ordering" parameter in the admin134 # options, then check the object's default ordering. If neither of135 # those exist, order descending by ID by default. Finally, look for136 # manually-specified ordering from the query string.137 ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]133 # For ordering, first check manually-specified ordering from the query 134 # string and let this override all other orderings. If there is no 135 # manually-specified ordering check the "ordering" parameter in the 136 # admin options, then check the object's default ordering. If neither 137 # of those exist, order descending by ID by default. 138 138 139 if ordering[0].startswith('-'): 140 order_field, order_type = ordering[0][1:], 'desc' 141 else: 142 order_field, order_type = ordering[0], 'asc' 143 if ORDER_VAR in params: 139 # Support multiple orderings by having order_field and order_type be 140 # lists such that order_field[i] and order_type[i] are corresponding 141 # order_field and order_type values. 142 143 order_field = [] 144 order_type = [] 145 # Check for manually-specified ordering from query string. 146 if ORDER_VAR in params and ORDER_TYPE_VAR in params: 144 147 try: 148 149 # Get order_type. If order_type is not 'asc' or 'desc' then 150 # raise ValueError to 'get out' of try block and use default 151 # ordering. 152 if params[ORDER_TYPE_VAR] in ('asc', 'desc'): 153 order_type.append(params[ORDER_TYPE_VAR]) 154 else: 155 raise ValueError 156 157 # Get order_field 145 158 field_name = self.list_display[int(params[ORDER_VAR])] 146 159 try: 147 160 f = lookup_opts.get_field(field_name) … … 155 168 attr = getattr(self.model_admin, field_name) 156 169 else: 157 170 attr = getattr(self.model, field_name) 158 order_field = attr.admin_order_field171 order_field.append(attr.admin_order_field) 159 172 except AttributeError: 160 173 pass 161 174 else: 162 order_field = f.name 175 order_field.append(f.name) 176 163 177 except (IndexError, ValueError): 164 178 pass # Invalid ordering specified. Just use the default. 165 if ORDER_TYPE_VAR in params and params[ORDER_TYPE_VAR] in ('asc', 'desc'): 166 order_type = params[ORDER_TYPE_VAR] 179 180 if not order_field or not order_type: 181 # Get ordering from "ordering" parameter in the admin options, 182 # object's default ordering, or by ID descending. 183 ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name] 184 for i in range(len(ordering)): 185 if ordering[i].startswith('-'): 186 order_field.append(ordering[i][1:]) 187 order_type.append('desc') 188 else: 189 order_field.append(ordering[i]) 190 order_type.append('asc') 191 167 192 return order_field, order_type 168 193 169 194 def get_query_set(self): … … 240 265 241 266 # Set ordering. 242 267 if self.order_field: 243 qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field)) 268 ordering = ['%s%s' % ((self.order_type[i] == 'desc' and '-' or ''), 269 self.order_field[i]) for i in range(len(self.order_field))] 270 qs = qs.order_by(*ordering) 244 271 245 272 # Apply keyword searches. 246 273 def construct_search(field_name):