[Django] #37024: Integer SITE_ID check incorrect when a different primary key type is used

3 views
Skip to first unread message

Django

unread,
Apr 7, 2026, 11:13:36 AM (yesterday) Apr 7
to django-...@googlegroups.com
#37024: Integer SITE_ID check incorrect when a different primary key type is used
-------------------------------------+-------------------------------------
Reporter: Tim | Owner: Tim Graham
Graham |
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: dev
contrib.sites |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
To catch a programmer mistake where the `SITE_ID` setting is defined with
an incorrect type (e.g. `SITE_ID = "1"`), #31802 added a system check that
only allows `SITE_ID` to be an `int` or `None`. This forces third-party
databases with non-integer primary keys like MongoDB (which uses
`ObjectId`) to silence this check.

It would be more appropriate if this check performed the validation based
on `Site._meta.pk` rather than with hardcoded types.

I thought of a few options:

1. Use `Site._meta.pk.to_python(settings.SITE_ID)` to validate the
`SITE_ID`, relying on it to raise `ValidationError` for unexpected types.
This won't work because `AutoField.to_python()` accepts strings that
coerce to integer which are values the original fix was intended to
reject.

2. Use `Site._meta.pk.to_python(settings.SITE_ID)` but also check if
`settings.SITE_ID != Site._meta.pk.to_python(settings.SITE_ID)`. This
would catch invalid values that `to_python()` coerces to the correct type.

3. Add `Field.expected_type` which could be `int`, `ObjectId`, or
whatever. The implementation of the check could continue to use
`isinstance()`. The downside is that adding a new attribute to the `Field`
API would be non-trivial.

I've implemented option #2.
--
Ticket URL: <https://code.djangoproject.com/ticket/37024>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 7, 2026, 11:18:11 AM (yesterday) Apr 7
to django-...@googlegroups.com
#37024: Integer SITE_ID check incorrect when a different primary key type is used
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
Type: | Graham
Cleanup/optimization | Status: assigned
Component: contrib.sites | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/21069 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/37024#comment:1>

Django

unread,
Apr 7, 2026, 3:01:48 PM (yesterday) Apr 7
to django-...@googlegroups.com
#37024: Integer SITE_ID check incorrect when a different primary key type is used
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
Type: | Graham
Cleanup/optimization | Status: assigned
Component: contrib.sites | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/37024#comment:2>

Django

unread,
3:37 PM (6 hours ago) 3:37 PM
to django-...@googlegroups.com
#37024: Integer SITE_ID check incorrect when a different primary key type is used
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Tim
Type: | Graham
Cleanup/optimization | Status: assigned
Component: contrib.sites | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MANAS MADESHIYA):

* cc: MANAS MADESHIYA (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/37024#comment:3>
Reply all
Reply to author
Forward
0 new messages