#31802 closed Cleanup/optimization (fixed)
Add system check for SITE_ID.
Reported by: | Gagan Deep | Owned by: | Parth Verma |
---|---|---|---|
Component: | contrib.sites | Version: | dev |
Severity: | Normal | Keywords: | sites, cache |
Cc: | me@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
The "sites" framework requires to set a SITE_ID
to specify the database ID of the Site object associated with that particular settings file. From the documentation, it can be interpreted that it should be an integer. But, it can also be set to a string literal('1'). I know this is wrong, but the Django accepted it and worked with it anyway without showing any warning or error.
The problem arose with SITE_CACHE
. It was not getting invalidated when the Site object is updated. The reason is the code which handles invalidation uses primary key of instance object which will be an integer. Therefore, the cache is never invalidated.
try: del SITE_CACHE[instance.pk] except KeyError: pass
Please see complete code on GitHub.
Since working of an element of Django is dependent on this setting, I suggest that there be a validation test for SITE_ID
.
Change History (7)
comment:2 by , 4 years ago
Summary: | Validation for "SITE_ID" setting → Add system check for SITE_ID. |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
Version: | 3.0 → master |
... it can be interpreted that it should be an integer.
It's clearly stated in docs: "The ID, as an integer, ..." but I agree that we can add a system check, e.g.
sites.E003: ``SITE_ID`` must be an integer.
comment:3 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 4 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Triage Stage: | Accepted → Ready for checkin |
comment:7 by , 4 years ago
It's clearly stated in docs: "The ID, as an integer, ..."
I must have overlooked that. Thanks for bringing it. :)
Part of the problem here is that
Site.objects.get(pk=some_string)
works because the database uses SQL implicit type conversion to cast the string, but we need a consistent value within Python.If it's only the site cache that's affected we could make
SITE_CACHE
a subclass of dict that casts keys but that seems like overengineering when a system check would provide enough validation at the boundary.