Skip to main content

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_pk as a primary key
  • Automatically search for the Book model across all installed Django apps
  • Load Book model and inject it into request.book
  • The model instance is loaded lazily — the database query runs only if request.book is 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

  1. Install package
pip install django-auto-model-loader
  1. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_auto_model_loader-1.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_auto_model_loader-1.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

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

Hashes for django_auto_model_loader-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b493e389466937a910c85e33fcb88904a4e8ccaa299ab13144f086f6f045dba3
MD5 5a80e1b2e9fd94730e1aad6fee8f96c1
BLAKE2b-256 f94885cb2d31423b71041ac440ab011bd977ad77aebb3e59fd76dad71975b563

See more details on using hashes here.

File details

Details for the file django_auto_model_loader-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auto_model_loader-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b84e94445d52ef1f5ff7a5fc00c219fbbc8a5893cec4869ad989b50de6c383ad
MD5 964bdf488b8cd018fd206e964ae49883
BLAKE2b-256 504c59660e192bed5d8ede55bcdd2d929dc3035d1b4a898b125fba17f871a139

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page