File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- from types import NoneType
2-
31from django .conf import settings
42from django .core .checks import Error
3+ from django .core .exceptions import ValidationError
54
65
76def 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 []
Original file line number Diff line number Diff line change @@ -967,7 +967,8 @@ The following checks are performed on any model using a
967967The following checks verify that :mod:`django.contrib.sites` is correctly
968968configured:
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---------------
Original file line number Diff line number Diff 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 ],
You can’t perform that action at this time.
0 commit comments