Django SQLite User DB (DSUD)
Disclaimer:
This package is purely experimental and is not intended for production use. Use at your own discretion.
Overview
Django SQLite User DB (DSUD) is a Django package that allows each user to have their own individual SQLite database.
Features
- Individual SQLite databases for each user
- On-demand db creation
- Migration handling
- Admin panel support
- Easy setup
Installation
To install DSUD, add the following modifications in your Django settings:
INSTALLED_APPS = [
...
'dsud',
...
]
MIDDLEWARE = [
...
"dsud.middleware.DatabaseHandlerMiddleware",
...
]
DATABASE_ROUTERS = ["dsud.routers.DatabaseRouter"]
TEST_RUNNER = "dsud.test_runner.TestRunner"
Dealing with migrations
To apply migrations to the user databases, django-sqlite-user-db provides a custom management command to apply migrations to all user dbs:
python src/manage.py migrate_user_dbs
This works by loading all the user entries in the root db, and applying migrations to them one by one.
Defining Users For Test Cases
In your settings, you can define test users by adding setting USER_SQLITE_DB_MANAGER_TEST_USER_DETAILS. This setting should contain a list of user payloads, such as:
USER_SQLITE_DB_MANAGER_TEST_USER_DETAILS = [
{"id": 1337, "username": "user_1337", "password": "secret"},
# Add more test users as needed
]
These users will be enrolled during the test runner's setup_database step, including the creation of their individual databases. They will also be automatically torn down after the tests are completed.
Admin Panel Integration
This assumes you've registered dsud.admin.UserAdmin for your User model, or inherit from it in a custom model admin and include "switch_user_button" in list_display.
To easily manage user databases through the Django admin panel, follow these steps:
-
Update
urls.py:Add the following path to your base
urls.pyfile:from django.urls import path, include urlpatterns = [ path( "admin/", include( "dsud.urls", namespace="dsud", ), ), ... ]
-
Customize Admin Templates:
Create a file at
templates/admin/base_site.htmlwith the following contents:{% extends "dsud/admin/base_site.html" %}
This will allow you to manage user-specific databases directly from the Django admin interface. Simply go to the users table, and click the "Switch user" button
Manual Database Entry
If you need to create entries in the user-specific database outside of requests (like test case setups for example), you can use the set_user_in_middleware context manager provided by DSUD. Here is an example:
from dsud.middleware import request_user_db
from your_app.models import Model
# Assuming you have a user instance
user = User.objects.get(username='example_user')
# Manually create entries in the user's database
with request_user_db(user):
obj1 = Model.objects.create(...)
obj2 = Model.objects.create(...)
This approach allows you to explicitly set the user context and perform database operations within that context, ensuring that the entries are created in the correct user-specific database.
Feel free to customize further based on your project's specific details!
Metadata
Release files for django-sqlite-user-db 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_sqlite_user_db-0.1.2.tar.gz | 10.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_sqlite_user_db-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.8 kB
Release files / django_sqlite_user_db-0.1.2.tar.gz
| Download URL | django_sqlite_user_db-0.1.2.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2100191cf22d0c1b43e1c18482f9b6fa684b69240d4027a0b9a1c24277fd5d62
|
|
BLAKE2b-256 checksum How to use checksums |
5b106566e17639b2f533915d6b0c7cc2f71c8a49bbd3913de2893fab6308fcd1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|
Release files / django_sqlite_user_db-0.1.2-py3-none-any.whl
| Download URL | django_sqlite_user_db-0.1.2-py3-none-any.whl |
|---|---|
| Size | 14.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a7c8e24a25e739bad36a1c42ab75e665c47a0f4fad9c04e8f608ceed2428b41b
|
|
BLAKE2b-256 checksum How to use checksums |
93566cbe7f477659927490e8494b6190cb3b3057e09b9ac3f6bd1895e9c37d71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|