Registry / testing / dj-inmemorystorage

dj-inmemorystorage

JSON →
library2.1.0pypypi✓ verified 90d ago

dj-inmemorystorage is a non-persistent in-memory data storage backend for Django, providing compatibility with Django's storage API. It is primarily used for testing purposes to avoid disk access and speed up test suites. The library is currently at version 2.1.0 and has an irregular release cadence, often tied to updates in Django and Python version support, as well as feature additions.

pip install dj-inmemorystorage
INSTALL
IMPORT
SIG · DJ-INMEMORYSTORAGE
D
dj-inmemorystorage
testingpythonv2.1.0
Install
3.5s avg
Import
379ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.915 runs
installs and imports cleanly · install 0.0s · import 0.399s · 66.4MB
glibc
py 3.10–3.915 runs
installs and imports cleanly · install 3.5s · import 0.359s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

InMemoryStorage
✓ from inmemorystorage import InMemoryStorage
While typically configured as a string in Django settings, this is the direct import path for programmatic use.

To use dj-inmemorystorage, configure your Django settings (typically in `settings.py` or a test settings file) by setting `DEFAULT_FILE_STORAGE` to `'inmemorystorage.InMemoryStorage'`. By default, the storage is non-persistent; enable `INMEMORYSTORAGE_PERSIST = True` if you need writes to be retained across operations within the same storage instance. The example demonstrates saving, checking existence, reading, and deleting a file using the configured default storage.

import os from django.conf import settings from django.core.files.storage import default_storage # Minimal Django settings for demonstration if not settings.configured: settings.configure( DEFAULT_FILE_STORAGE='inmemorystorage.InMemoryStorage', INMEMORYSTORAGE_PERSIST=True, # Optional: Set to True for persistence within a single storage instance SECRET_KEY='a-very-secret-key', DEBUG=True ) # Use the default storage to save and retrieve a file file_name = 'test_file.txt' file_content = b'Hello, in-memory storage!' # Save the file default_storage.save(file_name, file_content) print(f"File '{file_name}' saved to in-memory storage.") # Check if the file exists if default_storage.exists(file_name): print(f"File '{file_name}' exists.") # Read the file content with default_storage.open(file_name, 'rb') as f: content_read = f.read() print(f"Content of '{file_name}': {content_read.decode()}") # Delete the file default_storage.delete(file_name) print(f"File '{file_name}' deleted.") else: print(f"File '{file_name}' does not exist after saving, check persistence settings.")
Debug
Known issues
breakingVersion 2.0.0 introduced breaking changes by removing support for older Python and Django versions. Specifically, Python 2.6, 3.2, 3.3, 3.4 and Django versions older than 1.11 are no longer supported. Ensure your environment meets the new requirements (Python 3.7+, Django 2.2+).
fix
Upgrade Python to 3.7 or newer and Django to 2.2 or newer before upgrading dj-inmemorystorage to 2.0.0 or later.
affects: <2.0.0
gotchaBy default, `InMemoryStorage` is non-persistent. Writes made from one part of your code may not be accessible from another section if they are not sharing the same instance of the storage backend. This can lead to data loss if not explicitly handled.
fix
To enable persistence across operations within the same storage instance, set `INMEMORYSTORAGE_PERSIST = True` in your Django settings, e.g., `INMEMORYSTORAGE_PERSIST = True`.
affects: 1.3.0+
gotchaPrior to version 2.1.0, `InMemoryStorage` lacked the `@deconstruct` decorator and an `__eq__` method, which could cause issues with Django's class serialization and migration system, particularly when referencing the storage backend in model fields.
fix
Upgrade to `dj-inmemorystorage` version 2.1.0 or later to ensure proper compatibility with Django migrations and class serialization.
affects: <2.1.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'inmemorystorage' OR django.core.exceptions.ImproperlyConfigured: Could not find storage backend 'inmemorystorage.InMemoryStorage'.
The `dj-inmemorystorage` package is either not installed, or there is a typo in the `DEFAULT_FILE_STORAGE` setting.
fix
Ensure the package is installed: `pip install dj-inmemorystorage`. Double-check your `settings.py` for `DEFAULT_FILE_STORAGE = 'inmemorystorage.InMemoryStorage'` to ensure it's correctly spelled and pointing to the right location.
File operations (e.g., save, open) appear to work, but subsequent attempts to read/access the file return a 'PathDoesNotExist' error or indicate the file is missing.
The `InMemoryStorage` is non-persistent by default. If `INMEMORYSTORAGE_PERSIST` is not set to `True`, files are not guaranteed to be retained across different operations or instances of the storage backend.
fix
In your Django settings, add `INMEMORYSTORAGE_PERSIST = True` to enable persistence for the in-memory storage instance. Remember that even with persistence, data is lost when the application restarts.
django.db.migrations.exceptions.NodeNotFoundError: Migration ... depends on non-existent node ('inmemorystorage', '...') OR ValueError: Cannot serialize: <inmemorystorage.InMemoryStorage object at 0x...>
Older versions of `dj-inmemorystorage` (prior to 2.1.0) did not properly support Django's migration serialization due to missing `deconstruct` and `__eq__` methods on the `InMemoryStorage` class.
fix
Update `dj-inmemorystorage` to version 2.1.0 or newer. If issues persist, you might need to run `makemigrations` and `migrate` again after the upgrade, or consider manual migration adjustments if the storage was previously referenced in model fields.
Upgrade
Version history
2.1.0latest on PyPI · released Mar 30, 2020
Audit
Dependencies
DjangorequiredCore functionality for a Django storage backend.
sixrequiredCompatibility layer for Python 2 and 3, specified as a requirement in setup.py.
Agent activity
13 hits · last 30 days
node
10
Resources
dj-inmemorystorage — pip install dj-inmemorystorage · libregistry