Skip to content
Add logging of models from save, delete signals
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
models_logging
.gitignore
README.md
requirements.txt
setup.py

README.md

This package is for logging every changes in your models in this format:

[{"field": "price", "values": {"old": "2425", "new": "645"}}, {"field": "price_final", "values": {"old": "2425", "new": "645"}}]

USING

Add 'models_logging' at the end of INSTALLED_APPS in settings.py! This is important because models connect to save and init signals when apps is ready

INSTALLED_APPS = (
    ...,
    'models_logging',
)
  1. make migrations
  2. add the models you want to log in settings.py, format:
LOGGING_MODELS = (
    'app.ClassName',    # logging only for this model
    'another_app'     # logging of all models in this app
)

Sometimes object.save() operation can be called many times per 1 request. Per each .save() models_logging creates Change, so your database can quickly grow to a very large size for prevent this "bug" you can add middleware in settings.py

MIDDLEWARE = (
    ...,
    'models_logging.middleware.LoggingStackMiddleware',     # it merge all changes of object per request
)

or use context_manager from models_logging.utils in your view or script

from models_logging.utils import create_merged_changes

def your_script():
    with create_merged_changes():
        ...

You can add model for ignore logging settings.py

LOGGING_EXCLUDE = (
    'app'     # ignore logging of all models in this app
    'another_app.Model'     # ignore logging for this model
)

Also you can set up permission for the logging records Make func (it will be called in admin) or bool settings.py

def can_revert(request, obj):
    return request.user.username == 'myusername'

LOGGING_REVERT_IS_ALLOWED = can_revert
LOGGING_CAN_DELETE_REVISION = can_revert
LOGGING_CAN_DELETE_CHANGES = False
LOGGING_CAN_CHANGE_CHANGES = True

in models you can set attributes:

LOGGING_IGNORE_FIELDS = ()  # to ignore changes of some fields

# OR

LOGGING_ONLY_FIELDS  = ()    # to save changes of only those fields 

If you want to watch changes in admin/history of your object you can use models_logging.admin.HistoryAdmin

from models_logging.admin import HistoryAdmin


class YourAdminModel(HistoryAdmin):
    history_latest_first = False    # latest changes first
    inline_models_history = '__all__'   # __all__ or list of inline models for this ModelAdmin

Version > 1.0 is incompatible with old versions (requires django >= 2.0) for django <= 2.0 use 0.9.7 version

PS: It's not a backup your database, don't try to revert "huge" Revisions Recovering changes it's at least 1 hit to database (save or delete), if you run recovering thousands of changes this will be very slowly

You can’t perform that action at this time.