Skip to content

feat: implement autodiscover mechanism for automatic model registration #1504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Authors
- `Sridhar Marella <https://github.com/sridhar562345>`_
- `Mattia Fantoni <https://github.com/MattFanto>`_
- `Trent Holliday <https://github.com/trumpet2012>`_
- `Remigio Furini <https://github.com/remifu93>`_

Background
==========
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changes
Unreleased
----------

3.10.2 (2025-07-15)
-------------------

- Add autodiscovery mechanism to auto-register models in historical.py (gh-1504)

3.10.1 (2025-06-20)
-------------------

Expand Down
20 changes: 20 additions & 0 deletions docs/historical_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,23 @@ You will see the many to many changes when diffing between two historical record

# Output:
# categories changed from [{'poll': 1, 'category': 1}, { 'poll': 1, 'category': 2}] to [{'poll': 1, 'category': 2}]

Organizing history registrations with `historical.py`
-----------------------------------
To keep your project clean and maintainable, place all your `register()` calls
inside a dedicated `historical.py` file within each app.

With the autodiscover feature, `simple_history` automatically imports these
modules at Django startup, so you don’t need to manually import or register
your historical models elsewhere.

.. code-block:: python

# myapp/historical.py

from simple_history import register
from .models import Product


# Register models to track their history
register(Product, inherit=True)
3 changes: 3 additions & 0 deletions simple_history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ def register(
records.cls = model
records.add_extra_methods(model)
records.finalize(model)


default_app_config = "simple_history.apps.SimpleHistoryConfig"
11 changes: 11 additions & 0 deletions simple_history/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.apps import AppConfig


class SimpleHistoryConfig(AppConfig):
name = "simple_history"
verbose_name = "Simple History"

def ready(self):
from simple_history.utils import autodiscover_history_modules

autodiscover_history_modules()
20 changes: 20 additions & 0 deletions simple_history/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,23 @@ def get_change_reason_from_object(obj):
return getattr(obj, "_change_reason")

return None


def autodiscover_history_modules():
"""
Auto-import `<app>.historical` modules and fail silently if not present.
This triggers simple_history.register(...) calls inside those modules.
Typically called at app startup.
"""
from django.apps import apps
from django.utils.module_loading import import_module, module_has_submodule

for app_config in apps.get_app_configs():
module = f"{app_config.name}.historical"
try:
import_module(module)
except ImportError:
# Only re-raise if the module exists but failed to import
# Silently ignore if the module doesn't exist, as expected
if module_has_submodule(app_config.module, "historical"):
raise