Django middleware that automatically resolves model instances from URL parameters like user_pk
, book_pk
, etc., and injects them into the request
object.
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 intorequest.book
- The model instance is loaded lazily — the database query runs only if
request.book
is used.
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
- 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",
]
MIT © 2025 Dennis Tverdostup