Ticket #6786: choices_order.diff
File choices_order.diff, 4.0 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
82 82 max_length=None, unique=False, blank=False, null=False, db_index=False, 83 83 core=False, rel=None, default=NOT_PROVIDED, editable=True, serialize=True, 84 84 prepopulate_from=None, unique_for_date=None, unique_for_month=None, 85 unique_for_year=None, validator_list=None, choices=None, radio_admin=None,85 unique_for_year=None, validator_list=None, choices=None, order=None, radio_admin=None, 86 86 help_text='', db_column=None, db_tablespace=None): 87 87 self.name = name 88 88 self.verbose_name = verbose_name … … 101 101 self.unique_for_date, self.unique_for_month = unique_for_date, unique_for_month 102 102 self.unique_for_year = unique_for_year 103 103 self._choices = choices or [] 104 self._order = order or False 104 105 self.radio_admin = radio_admin 105 106 self.help_text = help_text 106 107 self.db_column = db_column … … 344 345 "Returns a list of tuples used as SelectField choices for this field." 345 346 first_choice = include_blank and blank_choice or [] 346 347 if self.choices: 347 return first_choice + list(self.choices) 348 rel_model = self.rel.to 349 if hasattr(self.rel, 'get_related_field'): 350 lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)] 348 lst = list(self.choices) 351 349 else: 352 lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)] 350 rel_qset = self.rel.to._default_manager.complex_filter(self.rel.limit_choices_to) 351 if hasattr(self.rel, 'get_related_field'): 352 lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_qset] 353 else: 354 lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_qset] 355 356 # Order choices list either ascending or descending, depending on sign of non-callable self._order, 357 # or using callable self._order as an argument for sort funciton 358 if self._order: 359 local_order = self._order 360 local_order = callable(local_order) and local_order or (lambda x,y: cmp(x.lower(), y.lower()) * self._order) 361 lst.sort(lambda x,y: local_order(force_unicode(x[1]), force_unicode(y[1]))) 362 353 363 return first_choice + lst 354 364 355 365 def get_choices_default(self): -
docs/model-api.txt
580 580 a proper database table with a ``ForeignKey``. ``choices`` is meant for static 581 581 data that doesn't change much, if ever. 582 582 583 ``order`` 584 ~~~~~~~~~~~ 585 586 **New in Django development version** 587 588 Specifies ordering for the choices list of this field (when ``choices`` 589 argument is specified), or for available primary key labels (if the field is 590 ForeignKey). Must be either an expression that could be evaluated as integer, 591 or callable. 592 593 If the argument is an expression that evaluates to 0 (default), ordering is 594 not performed and choices list appears in widgets as is. Positive value 595 represents ascending order, negative - descending. Both are case-insensitive. 596 597 To implement custom ordering, this argument must be callable, that is used by 598 Python sort method of list to compare two objects (see ``cmp`` in Python 599 documentation). The objects are converted to unicode before comparison. 600 601 Ordering is applied to the list each time widget is rendering. So, the list is 602 ordered after translation and localized lists are sorted correctly. At the 603 same time extensive usage of this argument may affect productivity. 604 583 605 ``core`` 584 606 ~~~~~~~~ 585 607