clear ContentType cache in import database#246
Merged
paigewilliams merged 2 commits intodevelopfrom Mar 18, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a post-database-import bug where Django’s in-process ContentType cache can retain stale content type PKs, causing resumable upload code paths to reference the wrong content_type_id after an import.
Changes:
- Clear Django’s
ContentTypein-memory cache immediately afterloaddatacompletes in the database import flow. - Add explanatory inline comments documenting the failure mode and why the cache clear is required.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+226
to
+232
| # Clear Django's in-memory ContentType cache so subsequent | ||
| # requests use the PKs from the newly imported data, not | ||
| # the stale pre-import values. Without this, the | ||
| # ResumableAdminWidget renders the wrong content_type_id | ||
| # (e.g. the old Media PK now maps to LookupTechniques), | ||
| # causing FieldDoesNotExist on upload. | ||
| ContentType.objects.clear_cache() |
Collaborator
Author
There was a problem hiding this comment.
@rhodges this copilot comment is kinda relevant. It's likely spot on with the "force an app reload/restart after successful import" comment.
Comment on lines
225
to
+232
| management.call_command("loaddata", fixture_file_path) | ||
| # Clear Django's in-memory ContentType cache so subsequent | ||
| # requests use the PKs from the newly imported data, not | ||
| # the stale pre-import values. Without this, the | ||
| # ResumableAdminWidget renders the wrong content_type_id | ||
| # (e.g. the old Media PK now maps to LookupTechniques), | ||
| # causing FieldDoesNotExist on upload. | ||
| ContentType.objects.clear_cache() |
rhodges
approved these changes
Mar 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I ran into a bug with interruptible uploads after doing an import of a database. The root cause of the problem was that the pk's for the
Mediatype were being cached, so that after doing an import of a database, the pks now change, and the cache is not cleared and thus referencing the pre-import pk.Here is an example of the steps I saw for the problem:
Because the import database functionality is TEK DB specific, I thought that clearing the cache was a preferred approach, rather than making the change in
django-resumable-async-upload, but I am open to other opinions.