From 63c56cda133a85a158502891c40465bc0331d3d9 Mon Sep 17 00:00:00 2001 From: Annabelle Wiegart Date: Sun, 18 Jan 2026 20:03:28 +0100 Subject: 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 --- tests/model_forms/test_modelchoicefield.py | 18 +++++++----- tests/model_forms/tests.py | 47 +++++++++++++++++------------- 2 files changed, 37 insertions(+), 28 deletions(-) (limited to 'tests/model_forms') diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 7f66b5b078..40c625da7c 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -2,6 +2,7 @@ import datetime from django import forms from django.core.exceptions import ValidationError +from django.db.models.utils import get_blank_choice_label from django.forms.models import ModelChoiceIterator, ModelChoiceIteratorValue from django.forms.widgets import CheckboxSelectMultiple from django.template import Context, Template @@ -24,7 +25,7 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual( list(f.choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), (self.c3.pk, "Third"), @@ -102,7 +103,7 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual( list(f.choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), ], @@ -118,7 +119,7 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual( list(gen_two), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), ], @@ -130,7 +131,7 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual( list(f.choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "category Entertainment"), (self.c2.pk, "category A test"), (self.c3.pk, "category Third"), @@ -143,7 +144,7 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual( list(f.choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), (self.c3.pk, "Third"), @@ -154,7 +155,7 @@ class ModelChoiceFieldTests(TestCase): self.assertEqual( list(f.choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), (self.c3.pk, "Third"), @@ -174,6 +175,7 @@ class ModelChoiceFieldTests(TestCase): self.assertIs(bool(f.choices), True) def test_choices_radio_blank(self): + blank_choice = [("", get_blank_choice_label())] choices = [ (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), @@ -190,7 +192,7 @@ class ModelChoiceFieldTests(TestCase): ) self.assertEqual( list(f.choices), - [("", "---------")] + choices if blank else choices, + (blank_choice + choices if blank else choices), ) def test_deepcopies_widget(self): @@ -424,7 +426,7 @@ class ModelChoiceFieldTests(TestCase): self.assertCountEqual( list(f.choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (self.c1.pk, "Entertainment"), (self.c2.pk, "A test"), (self.c3.pk, "Third"), diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 3d4eb06cf4..466b7dc57b 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -14,6 +14,7 @@ from django.core.exceptions import ( from django.core.files.uploadedfile import SimpleUploadedFile from django.db import connection, models from django.db.models.query import EmptyQuerySet +from django.db.models.utils import get_blank_choice_label from django.forms.models import ( ModelFormMetaclass, construct_instance, @@ -343,7 +344,7 @@ class ModelFormBaseTest(TestCase): self.assertEqual( list(form.fields["author"].choices), [ - ("", "---------"), + ("", get_blank_choice_label()), (writer.pk, "Joe Doe"), ], ) @@ -1531,7 +1532,7 @@ class ModelFormBasicTests(TestCase):
  • Slug:
  • Pub date:
  • Writer:
  • @@ -1543,7 +1544,7 @@ class ModelFormBasicTests(TestCase):
  • Status:
  • Writer:
  • @@ -1597,7 +1598,7 @@ class ModelFormBasicTests(TestCase):
  • Status: - + @@ -1748,7 +1749,7 @@ class ModelFormBasicTests(TestCase):
    Status: @@ -1781,7 +1782,7 @@ class ModelFormBasicTests(TestCase):
  • Pub date:
  • Writer:
  • @@ -1793,7 +1794,7 @@ class ModelFormBasicTests(TestCase):
  • Status:
  • ' '
  • Pub date:
  • ' '
  • Writer:
  • " @@ -1969,7 +1970,7 @@ class ModelFormBasicTests(TestCase): '' " " '
  • Status:
  • ' '
  • Pub date:
  • ' '
  • Writer:
  • " '
  • Status: - +

    @@ -2437,7 +2438,7 @@ class ModelOneToOneFieldTests(TestCase): """

    @@ -2727,7 +2728,8 @@ class FileAndImageFieldTests(TestCase): form = FPForm() self.assertEqual( - [name for _, name in form["path"].field.choices], ["---------", "models.py"] + [name for _, name in form["path"].field.choices], + [get_blank_choice_label(), "models.py"], ) @skipUnless(test_images, "Pillow not installed") @@ -3027,7 +3029,7 @@ class OtherModelFormTests(TestCase): self.assertEqual( tuple(field.choices), ( - ("", "---------"), + ("", get_blank_choice_label()), (multicolor_item.pk, "blue, red"), (red_item.pk, "red"), ), @@ -3041,14 +3043,19 @@ class OtherModelFormTests(TestCase): field = forms.ModelChoiceField(Inventory.objects.all(), to_field_name="barcode") self.assertEqual( tuple(field.choices), - (("", "---------"), (86, "Apple"), (87, "Core"), (22, "Pear")), + ( + ("", get_blank_choice_label()), + (86, "Apple"), + (87, "Core"), + (22, "Pear"), + ), ) form = InventoryForm(instance=core) self.assertHTMLEqual( str(form["parent"]), """