Ticket #6786: choices_order.diff

File choices_order.diff, 4.0 KB (added by David Avsajanishvili <avsd05@…>, 17 years ago)

changes in db.models.Field constructor and it's docs

  • 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, order=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._order = order 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        # 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
    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``order``
     584~~~~~~~~~~~
     585
     586**New in Django development version**
     587
     588Specifies ordering 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), ordering 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 ordering, 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
     601Ordering is applied to the list each time widget is rendering. So, the list is
     602ordered 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