diff options
| author | Tim Graham <timograham@gmail.com> | 2026-04-19 02:04:22 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-19 08:04:22 +0200 |
| commit | 179aa21b6dfeb9a0560a8c2bbfcf056301fc619f (patch) | |
| tree | f8d332194ace6d1e3958eb365310b82a869eb305 | |
| parent | aad47fe1bfc789f4b0ad0dc6373c945663e91f93 (diff) | |
Fixed #37024 -- Made SITE_ID system check validation use Site._meta.pk.
| -rw-r--r-- | django/contrib/sites/checks.py | 32 | ||||
| -rw-r--r-- | docs/ref/checks.txt | 3 | ||||
| -rw-r--r-- | tests/sites_tests/tests.py | 17 |
3 files changed, 41 insertions, 11 deletions
diff --git a/django/contrib/sites/checks.py b/django/contrib/sites/checks.py index 66e8551bed..bf7c9b7dce 100644 --- a/django/contrib/sites/checks.py +++ b/django/contrib/sites/checks.py @@ -1,14 +1,30 @@ -from types import NoneType - from django.conf import settings from django.core.checks import Error +from django.core.exceptions import ValidationError def check_site_id(app_configs, **kwargs): - if hasattr(settings, "SITE_ID") and not isinstance( - settings.SITE_ID, (NoneType, int) - ): - return [ - Error("The SITE_ID setting must be an integer", id="sites.E101"), - ] + # Inner import avoids AppRegistryNotReady + from django.contrib.sites.models import Site + + if hasattr(settings, "SITE_ID"): + try: + site_id = Site._meta.pk.to_python(settings.SITE_ID) + except ValidationError as exc: + return [ + Error( + f"The SITE_ID setting failed to validate: {exc}.", id="sites.E101" + ), + ] + else: + # to_python() might coerce a SITE_ID of the wrong type to the valid + # type, e.g. "1" to 1 for AutoField. + if site_id != settings.SITE_ID: + expected_type = type(site_id).__name__ + return [ + Error( + f"The SITE_ID setting must be of type {expected_type}.", + id="sites.E101", + ), + ] return [] diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index 16f0c5dac4..fb9187c2ac 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -967,7 +967,8 @@ The following checks are performed on any model using a The following checks verify that :mod:`django.contrib.sites` is correctly configured: -* **sites.E101**: The :setting:`SITE_ID` setting must be an integer. +* **sites.E101**: The :setting:`SITE_ID` setting must be of type ``<type>``. + *or* The :setting:`SITE_ID` setting failed to validate: ``<error>``. ``staticfiles`` --------------- diff --git a/tests/sites_tests/tests.py b/tests/sites_tests/tests.py index 79183f7aca..32bc8f65f9 100644 --- a/tests/sites_tests/tests.py +++ b/tests/sites_tests/tests.py @@ -204,12 +204,25 @@ class SitesFrameworkTests(TestCase): self.assertEqual(self.site.natural_key(), (self.site.domain,)) @override_settings(SITE_ID="1") - def test_check_site_id(self): + def test_check_site_id_incorrect_type(self): self.assertEqual( check_site_id(None), [ checks.Error( - msg="The SITE_ID setting must be an integer", + msg="The SITE_ID setting must be of type int.", + id="sites.E101", + ), + ], + ) + + @override_settings(SITE_ID="x") + def test_check_site_id_failed_to_validate(self): + self.assertEqual( + check_site_id(None), + [ + checks.Error( + msg="The SITE_ID setting failed to validate: ['“x” value " + "must be an integer.'].", id="sites.E101", ), ], |
