Ticket #6786: choices_sort.diff

File choices_sort.diff, 3.9 KB (added by David Avsajanishvili <avsd05@…>, 16 years ago)

Renamed parameter "order" to "sort"

  • django/db/models/fields/__init__.py

     
    8282        max_length=None, unique=False, blank=False, null=False, db_index=False,
    8383        core=False, rel=None, default=NOT_PROVIDED, editable=True, serialize=True,
    8484        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, sort=None, radio_admin=None,
    8686        help_text='', db_column=None, db_tablespace=None):
    8787        self.name = name
    8888        self.verbose_name = verbose_name
     
    101101        self.unique_for_date, self.unique_for_month = unique_for_date, unique_for_month
    102102        self.unique_for_year = unique_for_year
    103103        self._choices = choices or []
     104        self._sort = sort or False
    104105        self.radio_admin = radio_admin
    105106        self.help_text = help_text
    106107        self.db_column = db_column
     
    344345        "Returns a list of tuples used as SelectField choices for this field."
    345346        first_choice = include_blank and blank_choice or []
    346347        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)
    351349        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        # Sort choices list either ascending or descending, depending on sign of non-callable self._sort,
     357        # or using callable self._sort as an argument for sort funciton
     358        if self._sort:
     359            local_sort = self._sort
     360            local_sort = callable(local_sort) and local_sort or (lambda x,y: cmp(x.lower(), y.lower()) * self._sort)
     361            lst.sort(lambda x,y: local_sort(force_unicode(x[1]), force_unicode(y[1])))
     362
    353363        return first_choice + lst
    354364
    355365    def get_choices_default(self):
  • docs/model-api.txt

     
    580580a proper database table with a ``ForeignKey``. ``choices`` is meant for static
    581581data that doesn't change much, if ever.
    582582
     583``sort``
     584~~~~~~~~~~~
     585
     586**New in Django development version**
     587
     588Specifies sorting for the choices list of this field (when ``choices``
     589argument is specified), or for available primary key labels (if the field is
     590ForeignKey). Must be either an expression that could be evaluated as integer,
     591or callable.
     592
     593If the argument is an expression that evaluates to 0 (default), sorting is
     594not performed and choices list appears in widgets as is. Positive value
     595represents ascending order, negative - descending. Both are case-insensitive.
     596
     597To implement custom sorting, this argument must be callable, that is used by
     598Python sort method of list to compare two objects (see ``cmp`` in Python
     599documentation). The objects are converted to unicode before comparison.
     600
     601Sorting is applied to the list each time widget is rendering. So, the list is
     602sorted after translation and localized lists are sorted correctly. At the
     603same time extensive usage of this argument may affect productivity.
     604
    583605``core``
    584606~~~~~~~~
    585607
Back to Top