Skip to content

Commit 179aa21

Browse files
authored
Fixed #37024 -- Made SITE_ID system check validation use Site._meta.pk.
1 parent aad47fe commit 179aa21

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

django/contrib/sites/checks.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
from types import NoneType
2-
31
from django.conf import settings
42
from django.core.checks import Error
3+
from django.core.exceptions import ValidationError
54

65

76
def check_site_id(app_configs, **kwargs):
8-
if hasattr(settings, "SITE_ID") and not isinstance(
9-
settings.SITE_ID, (NoneType, int)
10-
):
11-
return [
12-
Error("The SITE_ID setting must be an integer", id="sites.E101"),
13-
]
7+
# Inner import avoids AppRegistryNotReady
8+
from django.contrib.sites.models import Site
9+
10+
if hasattr(settings, "SITE_ID"):
11+
try:
12+
site_id = Site._meta.pk.to_python(settings.SITE_ID)
13+
except ValidationError as exc:
14+
return [
15+
Error(
16+
f"The SITE_ID setting failed to validate: {exc}.", id="sites.E101"
17+
),
18+
]
19+
else:
20+
# to_python() might coerce a SITE_ID of the wrong type to the valid
21+
# type, e.g. "1" to 1 for AutoField.
22+
if site_id != settings.SITE_ID:
23+
expected_type = type(site_id).__name__
24+
return [
25+
Error(
26+
f"The SITE_ID setting must be of type {expected_type}.",
27+
id="sites.E101",
28+
),
29+
]
1430
return []

docs/ref/checks.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,8 @@ The following checks are performed on any model using a
967967
The following checks verify that :mod:`django.contrib.sites` is correctly
968968
configured:
969969

970-
* **sites.E101**: The :setting:`SITE_ID` setting must be an integer.
970+
* **sites.E101**: The :setting:`SITE_ID` setting must be of type ``<type>``.
971+
*or* The :setting:`SITE_ID` setting failed to validate: ``<error>``.
971972

972973
``staticfiles``
973974
---------------

tests/sites_tests/tests.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,25 @@ def test_site_natural_key(self):
204204
self.assertEqual(self.site.natural_key(), (self.site.domain,))
205205

206206
@override_settings(SITE_ID="1")
207-
def test_check_site_id(self):
207+
def test_check_site_id_incorrect_type(self):
208208
self.assertEqual(
209209
check_site_id(None),
210210
[
211211
checks.Error(
212-
msg="The SITE_ID setting must be an integer",
212+
msg="The SITE_ID setting must be of type int.",
213+
id="sites.E101",
214+
),
215+
],
216+
)
217+
218+
@override_settings(SITE_ID="x")
219+
def test_check_site_id_failed_to_validate(self):
220+
self.assertEqual(
221+
check_site_id(None),
222+
[
223+
checks.Error(
224+
msg="The SITE_ID setting failed to validate: ['“x” value "
225+
"must be an integer.'].",
213226
id="sites.E101",
214227
),
215228
],

0 commit comments

Comments
 (0)