Middleware for auto-loading Django models from URL parameters
Project description
django-auto-model-loader
Django middleware that automatically resolves model instances from URL parameters like user_pk, book_pk, etc., and injects them into the request object.
Usage
Basic Example
For a URL like:
/api/books/<int:book_pk>/
The middleware will:
- Detect
book_pkas a primary key - Automatically search for the
Bookmodel across all installed Django apps - Load
Bookmodel and inject it intorequest.book - The model instance is loaded lazily — the database query runs only if
request.bookis used.
Using Aliases
You can also register custom aliases for models if the default name doesn't match the actual class name.
from django.db import models
from django_auto_model_loader import model_alias
@model_alias("example_book")
class Book(models.Model):
title = models.CharField(max_length=255)
author = models.CharField(max_length=255)
This allows the middleware to resolve both of the following:
/api/books/<int:book_pk>/→request.book/api/books/<int:example_book_pk>/→request.book
⚙️ Installation
Setup
- Install package
pip install django-auto-model-loader
- Add middleware to your Django settings:
INSTALLED_APPS = [
...,
"django_auto_model_loader",
]
MIDDLEWARE = [
...,
"django_auto_model_loader.middleware.AutoModelLoaderMiddleware",
]
📜 License
MIT © 2025 Dennis Tverdostup
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_auto_model_loader-1.0.1.tar.gz.
File metadata
- Download URL: django_auto_model_loader-1.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b493e389466937a910c85e33fcb88904a4e8ccaa299ab13144f086f6f045dba3
|
|
| MD5 |
5a80e1b2e9fd94730e1aad6fee8f96c1
|
|
| BLAKE2b-256 |
f94885cb2d31423b71041ac440ab011bd977ad77aebb3e59fd76dad71975b563
|
File details
Details for the file django_auto_model_loader-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_auto_model_loader-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b84e94445d52ef1f5ff7a5fc00c219fbbc8a5893cec4869ad989b50de6c383ad
|
|
| MD5 |
964bdf488b8cd018fd206e964ae49883
|
|
| BLAKE2b-256 |
504c59660e192bed5d8ede55bcdd2d929dc3035d1b4a898b125fba17f871a139
|