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.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-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file django-auto-model-loader-1.0.tar.gz.

File metadata

  • Download URL: django-auto-model-loader-1.0.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.tar.gz
Algorithm Hash digest
SHA256 1a39020aa05362ff26f9921fd66341689362c2271e8a86033bd37bb3b13d0ef0
MD5 a2c0bd412050da03c7e7f1f6e029406e
BLAKE2b-256 5756bf5543be772ec3301a514e5ea2f91bf4b4f219bc137f2bd2700ff3635e20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_auto_model_loader-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 590e9d0b17bb652540ab2291e2f2fb2e13a3609eb31b3216c2c61ead80e4ef01
MD5 8389e7b7efe8d6e12315b359afa2fa90
BLAKE2b-256 b27a674b000e826b8814a9e3c82426f68d8611d7b4703441377de7d80f9ec48b

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