summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorAnnabelle Wiegart <annabelle.wiegart@proton.me>2026-01-18 20:03:28 +0100
committerJacob Walls <jacobtylerwalls@gmail.com>2026-04-22 17:06:29 -0400
commit63c56cda133a85a158502891c40465bc0331d3d9 (patch)
tree04380903d14307b71416b2e048ce4be8361cf0df /django/db
parentdc467fdc3b5744cec71fab876c23a14013e2510b (diff)
Fixed #35870 -- Made blank choice label in forms more accessible.
Added new constant django.db.models.fields.BLANK_CHOICE_LABEL for an accessible and translatable blank choice label in forms. Deprecated django.db.models.fields.BLANK_CHOICE_DASH constant. Added the immediately deprecated transitional setting USE_BLANK_CHOICE_DASH. Co-Authored-By: Marijke Luttekes <mail@marijkeluttekes.dev>
Diffstat (limited to 'django/db')
-rw-r--r--django/db/models/fields/__init__.py15
-rw-r--r--django/db/models/fields/reverse_related.py6
-rw-r--r--django/db/models/utils.py12
3 files changed, 28 insertions, 5 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index e248b70ba3..a7ec41bf75 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -15,6 +15,7 @@ from django.core import checks, exceptions, validators
from django.db import connection, connections, router
from django.db.models.constants import LOOKUP_SEP
from django.db.models.query_utils import DeferredAttribute, RegisterLookupMixin
+from django.db.models.utils import get_blank_choice_label
from django.db.utils import NotSupportedError
from django.utils import timezone
from django.utils.choices import (
@@ -39,7 +40,9 @@ from django.utils.translation import gettext_lazy as _
__all__ = [
"AutoField",
+ # RemovedInDjango70Warning
"BLANK_CHOICE_DASH",
+ "BLANK_CHOICE_LABEL",
"BigAutoField",
"BigIntegerField",
"BinaryField",
@@ -81,9 +84,13 @@ class NOT_PROVIDED:
pass
-# The values to use for "blank" in SelectFields. Will be appended to the start
-# of most "choices" lists.
+# RemovedInDjango70Warning: From Django 6.1, the values to use for "blank"
+# in SelectFields will be defined by the below BLANK_CHOICE_LABEL constant.
+# Will be appended to the start of most "choices" lists.
+# BLANK_CHOICE_DASH is still available as a constant in Django 6.1.
BLANK_CHOICE_DASH = [("", "---------")]
+# This allows any app's ready() method to overwrite BLANK_CHOICE_LABEL.
+BLANK_CHOICE_LABEL = _("- Select an option -")
def _load_field(app_label, model_name, field_name):
@@ -1088,7 +1095,7 @@ class Field(RegisterLookupMixin):
def get_choices(
self,
include_blank=True,
- blank_choice=BLANK_CHOICE_DASH,
+ blank_choice=None,
limit_choices_to=None,
ordering=(),
):
@@ -1096,6 +1103,8 @@ class Field(RegisterLookupMixin):
Return choices with a default blank choices included, for use
as <select> choices for this field.
"""
+ if blank_choice is None:
+ blank_choice = [("", get_blank_choice_label())]
if self.choices is not None:
if include_blank:
return BlankChoiceIterator(self.choices, blank_choice)
diff --git a/django/db/models/fields/reverse_related.py b/django/db/models/fields/reverse_related.py
index e6c2525115..df50ba436f 100644
--- a/django/db/models/fields/reverse_related.py
+++ b/django/db/models/fields/reverse_related.py
@@ -13,7 +13,7 @@ from django.core import exceptions
from django.utils.functional import cached_property
from django.utils.hashable import make_hashable
-from . import BLANK_CHOICE_DASH
+from ..utils import get_blank_choice_label
from .mixins import FieldCacheMixin
@@ -172,7 +172,7 @@ class ForeignObjectRel(FieldCacheMixin):
def get_choices(
self,
include_blank=True,
- blank_choice=BLANK_CHOICE_DASH,
+ blank_choice=None,
limit_choices_to=None,
ordering=(),
):
@@ -183,6 +183,8 @@ class ForeignObjectRel(FieldCacheMixin):
Analog of django.db.models.fields.Field.get_choices(), provided
initially for utilization by RelatedFieldListFilter.
"""
+ if blank_choice is None:
+ blank_choice = [("", get_blank_choice_label())]
limit_choices_to = limit_choices_to or self.limit_choices_to
qs = self.related_model._default_manager.complex_filter(limit_choices_to)
if ordering:
diff --git a/django/db/models/utils.py b/django/db/models/utils.py
index c6cb5ef165..7f38bd7afb 100644
--- a/django/db/models/utils.py
+++ b/django/db/models/utils.py
@@ -67,3 +67,15 @@ class AltersData:
break
super().__init_subclass__(**kwargs)
+
+
+# RemovedInDjango70Warning: At the end of the deprecation, remove this function
+# and use .fields.BLANK_CHOICE_LABEL directly instead.
+def get_blank_choice_label():
+ from django.conf import settings
+
+ from .fields import BLANK_CHOICE_DASH, BLANK_CHOICE_LABEL
+
+ if settings.USE_BLANK_CHOICE_DASH:
+ return BLANK_CHOICE_DASH[0][1]
+ return BLANK_CHOICE_LABEL