From 31837dbcb36f1ab57fb1b16cb0b126c55a1bdf01 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 3 Jul 2024 07:51:50 -0400 Subject: [PATCH] Fixed #35569 -- Improved wording of invalid ForeignKey error message. --- django/db/models/fields/related.py | 4 +++- tests/auth_tests/test_management.py | 8 ++++---- tests/validation/tests.py | 9 ++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 7d42d1ea38..8930d6ef4a 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -929,7 +929,9 @@ class ForeignKey(ForeignObject): empty_strings_allowed = False default_error_messages = { - "invalid": _("%(model)s instance with %(field)s %(value)r does not exist.") + "invalid": _( + "%(model)s instance with %(field)s %(value)r is not a valid choice." + ) } description = _("Foreign Key (type determined by related field)") diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index 5765c50034..8dd91cf6ed 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -523,7 +523,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase): self.assertEqual(u.group, group) non_existent_email = "mymail2@gmail.com" - msg = "email instance with email %r does not exist." % non_existent_email + msg = "email instance with email %r is not a valid choice." % non_existent_email with self.assertRaisesMessage(CommandError, msg): call_command( "createsuperuser", @@ -594,7 +594,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase): email = Email.objects.create(email="mymail@gmail.com") Group.objects.all().delete() nonexistent_group_id = 1 - msg = f"group instance with id {nonexistent_group_id} does not exist." + msg = f"group instance with id {nonexistent_group_id} is not a valid choice." with self.assertRaisesMessage(CommandError, msg): call_command( @@ -611,7 +611,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase): email = Email.objects.create(email="mymail@gmail.com") Group.objects.all().delete() nonexistent_group_id = 1 - msg = f"group instance with id {nonexistent_group_id} does not exist." + msg = f"group instance with id {nonexistent_group_id} is not a valid choice." with mock.patch.dict( os.environ, @@ -631,7 +631,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase): email = Email.objects.create(email="mymail@gmail.com") Group.objects.all().delete() nonexistent_group_id = 1 - msg = f"group instance with id {nonexistent_group_id} does not exist." + msg = f"group instance with id {nonexistent_group_id} is not a valid choice." @mock_inputs( { diff --git a/tests/validation/tests.py b/tests/validation/tests.py index 6bb04f6f14..494310e553 100644 --- a/tests/validation/tests.py +++ b/tests/validation/tests.py @@ -31,15 +31,18 @@ class BaseModelValidationTests(ValidationAssertions, TestCase): self.assertFieldFailsValidationWithMessage( mtv.full_clean, "parent", - ["model to validate instance with id %r does not exist." % mtv.parent_id], + [ + "model to validate instance with id %r is not a valid choice." + % mtv.parent_id + ], ) mtv = ModelToValidate(number=10, name="Some Name", ufm_id="Some Name") self.assertFieldFailsValidationWithMessage( mtv.full_clean, "ufm", [ - "unique fields model instance with unique_charfield %r does not exist." - % mtv.name + "unique fields model instance with unique_charfield %r is not " + "a valid choice." % mtv.name ], )