Commit 1123f455 authored by Alex Couper's avatar Alex Couper Committed by Tim Graham
Browse files

Fixed #20649 -- Allowed blank field display to be defined in the initial list of choices.

parent a1889397
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -161,6 +161,7 @@ answer newbie questions, and generally made Django that much better:
    Paul Collier <paul@paul-collier.com>
    Paul Collins <paul.collins.iii@gmail.com>
    Robert Coup
    Alex Couper <http://alexcouper.com/>
    Deric Crago <deric.crago@gmail.com>
    Brian Fabian Crain <http://www.bfc.do/>
    David Cramer <dcramer@gmail.com>
+8 −1
Original line number Diff line number Diff line
@@ -544,7 +544,14 @@ class Field(object):
    def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
        """Returns choices with a default blank choices included, for use
        as SelectField choices for this field."""
        first_choice = blank_choice if include_blank else []
        blank_defined = False
        for choice, _ in self.choices:
            if choice in ('', None):
                blank_defined = True
                break

        first_choice = (blank_choice if include_blank and
                        not blank_defined else [])
        if self.choices:
            return first_choice + list(self.choices)
        rel_model = self.rel.to
+2 −0
Original line number Diff line number Diff line
@@ -511,6 +511,8 @@ class Select(Widget):
        return mark_safe('\n'.join(output))

    def render_option(self, selected_choices, option_value, option_label):
        if option_value == None:
            option_value = ''
        option_value = force_text(option_value)
        if option_value in selected_choices:
            selected_html = mark_safe(' selected="selected"')
+14 −5
Original line number Diff line number Diff line
@@ -152,11 +152,20 @@ method to retrieve the human-readable name for the field's current value. See
:meth:`~django.db.models.Model.get_FOO_display` in the database API
documentation.

Finally, note that choices can be any iterable object -- not necessarily a list
or tuple. This lets you construct choices dynamically. But if you find yourself
hacking :attr:`~Field.choices` to be dynamic, you're probably better off using a
proper database table with a :class:`ForeignKey`. :attr:`~Field.choices` is
meant for static data that doesn't change much, if ever.
Note that choices can be any iterable object -- not necessarily a list or tuple.
This lets you construct choices dynamically. But if you find yourself hacking
:attr:`~Field.choices` to be dynamic, you're probably better off using a proper
database table with a :class:`ForeignKey`. :attr:`~Field.choices` is meant for
static data that doesn't change much, if ever.

.. versionadded:: 1.7

Unless :attr:`blank=False<Field.blank>` is set on the field along with a
:attr:`~Field.default` then a label containing ``"---------"`` will be rendered
with the select box. To override this behavior, add a tuple to ``choices``
containing ``None``; e.g. ``(None, 'Your String For Display')``.
Alternatively, you can use an empty string instead of ``None`` where this makes
sense - such as on a :class:`~django.db.models.CharField`.

``db_column``
-------------
+5 −0
Original line number Diff line number Diff line
@@ -105,6 +105,11 @@ Minor features
  <django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed>` method
  to more easily customize the login policy.

* :attr:`Field.choices<django.db.models.Field.choices>` now allows you to
  customize the "empty choice" label by including a tuple with an empty string
  or ``None`` for the key and the custom label as the value. The default blank
  option ``"----------"`` will be omitted in this case.

Backwards incompatible changes in 1.7
=====================================

Loading