Skip to main content

Save model history

Project description

django-model-history-log

Register fields updates.

Install

Add model_history.apps.ModelHistoryConfig into your INSTALLED_APPS

#!python
INSTALLED_APPS = [
    ...
    "model_history.apps.ModelHistoryConfig",
    ...
]

and than run

./manage.py migrate

Configure

Register a model at startup

You can register a model for logging at startup using History.register helper:

class MyAppConfig(AppConfig):
    name = "myapp"

    def ready(self):
        from model_history.models import History
        from django.contrib.auth import get_user_model

        History.register(get_user_model(), exclude=["password"])

You can register all models with a snippet like this

class MyAppConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "myapp"
    verbose_name = _("My Website")

    def ready(self):
        from django.contrib.admin.models import LogEntry
        from django.contrib.auth import get_user_model
        from django.contrib.sessions.models import Session
        from model_history.models import History, HistoryLog

        User = get_user_model()
        History.register(User, exclude=["password"])
        for model in apps.get_models():
            if model not in [LogEntry, History, HistoryLog, Session, User]:
                History.register(model)

Usage

Log a single instance

You can log a single instance changes with the HistoryManager.log() method:

instance = MyInstance.objects.get(...)
...
History.objects.log(instance)
Params
  • instance: models.Model = instance to log
  • exclude: list[str] | None = exclude these fields from logging
  • serializer_class: rest_framework.serializers.Serializer | None = use this serializer instance

Query a log

    from django.contrib.auth import get_user_model

    history = History.objects.fetch(User.objects.first())
    history.logs.all()

fetch() always returns an History instance, regardless it is saved on db or not. You must rely on it's pk value or you should check for logs.

CHANGES

0.2.0

  • Rename project to django-model-history-log (sorry, no migration path)
  • Reformat all codebase with black
  • Update isort/flake8 config
  • Use django JSONField
  • Remove default ordering, use only in admin
  • Add register/unregister actions
  • Add HistoryQuerySet.fetch api
  • Save str(source) as {History,HistoryLog}.label
  • Add en translations
  • Add it translations

0.1.1

  • added missing on_delete on HistoryRow.history field

0.1.0

  • initial relase

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-model-history-log-0.2.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

django_model_history_log-0.2-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file django-model-history-log-0.2.tar.gz.

File metadata

File hashes

Hashes for django-model-history-log-0.2.tar.gz
Algorithm Hash digest
SHA256 dd1e704fe9587fe2a50b5b3379aa2ff7466b084c9845fcca7f8b3036535410ca
MD5 2eb5a2ccb2fab5e95544e0d921ff8226
BLAKE2b-256 38e26e178422c4cd4b0aca55654b590363e41d02cd40a62691a628f2a7af6b0e

See more details on using hashes here.

File details

Details for the file django_model_history_log-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_history_log-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1bc2187511d2a1351694cbc2123c50fbec9d9b9b1b31d8fba9edf019f0938f42
MD5 feddf0e2ae1bad2693e8322496f0e9bc
BLAKE2b-256 4f47b1984cd27a35ca2e4a49d3809199b455a3acab5fb3e7a4c58108adab9488

See more details on using hashes here.

Supported by

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