Created
March 25, 2015 13:34
-
-
Save cstrap/add7db86d43f98d0df0f to your computer and use it in GitHub Desktop.
Django model log history. A sample that uses LogEntry (django default) and reversion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.contrib.admin.models import LogEntry, CHANGE, ContentType | |
| from reversion.revisions import default_revision_manager as revision_manager | |
| from app.models import MyModel | |
| revision_manager.register(MyModel) | |
| LogUser, created = User.objects.get_or_create(username='LogUser') | |
| if created: | |
| ApplicationUser.first_name = 'Log' | |
| ApplicationUser.last_name = 'User' | |
| ApplicationUser.email = settings.ADMINS[0][-1] | |
| ApplicationUser.is_staff = False | |
| ApplicationUser.is_active = True | |
| ApplicationUser.is_superuser = False | |
| ApplicationUser.set_password('password') # just for sample ... | |
| ApplicationUser.save() | |
| user = ApplicationUser # or it can be request.user ... | |
| def log_admin(model, message): | |
| # Django admin log | |
| # see use on django.contrib.admin.options | |
| LogEntry.objects.log_action( | |
| user_id=user.pk, | |
| content_type_id=ContentType.objects.get_for_model(model).pk, | |
| object_id=model.pk, | |
| object_repr=force_unicode(model), | |
| action_flag=CHANGE, | |
| change_message=message | |
| ) | |
| # Reversion admin log, needed if model is registered on reversion | |
| revision_manager.save_revision([model], ignore_duplicates=True, user=user, comment=message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment