# django-model-tracker
Track model object changes over time so that you know who done what.
[![PyPI version](https://badge.fury.io/py/django-model-tracker.svg)](https://badge.fury.io/py/django-model-tracker)
[![Downloads](https://pepy.tech/badge/django-model-tracker)](https://pepy.tech/project/django-model-tracker)
## Installation
* Install the package
* For Django<4.0
```sh
pip install 'django-model-tracker jsonfield'
```
* For Django>=4.0
```sh
pip install 'django-model-tracker'
```
* Add Application to your project's INSTALLED_APPs
```python
INSTALLED_APPS = (
'....',
'ModelTracker',
)
```
* Add the following line to your urls.py
```python
import ModelTracker
urlpatterns = patterns('',
...
url(r'^track/', include('ModelTracker.urls')),
...
)
```
* Run Migrations
```sh
python manage.py migrate ModelTracker
```
* Add the following line to your models.py file
```python
from ModelTracker import Tracker
```
* Convert each Model you want to track to inhert from `Tracker.ModelTracker` instead of `models.Model`
**Old Code**
```python
class Employee(models.Model):
name=models.CharField(max_length=255)
address=models.CharField(max_length=255)
age=models.IntegerField()
```
**New Code**
```python
class Employee(Tracker.ModelTracker):
name=models.CharField(max_length=255)
address=models.CharField(max_length=255)
age=models.IntegerField()
```
* For each save() call, add the user the username
* Old Code
```python
emp=Employee()
emp.save()
```
* New Code
```python
emp=Employee()
emp.save(request.user.username)
```
* Starting from version of 0.5, you can pass a event_name parameter to mark change as an event
* New Code
```python
emp=Employee()
emp.save(request.user.username,event_name="Created the user")
```
# Using The Middleware
You can add `ModelTracker.middleware.ModelTrackerMiddleware` to your Middleware classes to get the username automatically from the request.
```python
MIDDLEWARE_CLASSES = (
'....',
'ModelTracker.middleware.ModelTrackerMiddleware',
)
```
**Note:** If you pass username as `None` then the change won't be saved.
# Showing Record History
There are 3 ways to see the history of a record
1. go to `ModelTracker` url and select `Table` and enter `id`.
2. call `showModelChanges` by POST and send `csrftokenmiddleware` to return history as html.
3. call `getModelChanges` which returns history as Json.
# Django Admin
There is 2 ways to update an object by django admin
1. Handle save and delete in ModelAdmin as follows
```python
def save_model(self, request, obj, form, change):
obj.save(request.user.username,"Editing From admin interface")
def delete_model(self, request, obj):
obj.delete(username=request.user.username, event_name="Deleting From admin interface")
```
2. Inhert from TrackerAdmin rather ModelAdmin
```python
from ModelTracker.Tracker import TrackerAdmin
admin.site.register(employee, TrackerAdmin)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/mkalioby/ModelTracker/",
"name": "django-model-tracker",
"maintainer": "",
"docs_url": null,
"requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
"maintainer_email": "",
"keywords": "",
"author": "Mohamed El-Kalioby",
"author_email": "mkalioby@mkalioby.com",
"download_url": "https://files.pythonhosted.org/packages/3d/55/1c3a34615abc5474a65218a6b18884c3a8803dcaab12128c78efac4d7ab6/django-model-tracker-2.0.4.tar.gz",
"platform": null,
"description": "# django-model-tracker\n\nTrack model object changes over time so that you know who done what.\n\n[![PyPI version](https://badge.fury.io/py/django-model-tracker.svg)](https://badge.fury.io/py/django-model-tracker)\n[![Downloads](https://pepy.tech/badge/django-model-tracker)](https://pepy.tech/project/django-model-tracker)\n \n## Installation\n\n* Install the package\n * For Django<4.0\n ```sh\n pip install 'django-model-tracker jsonfield'\n ```\n * For Django>=4.0\n ```sh\n pip install 'django-model-tracker'\n ```\n\n* Add Application to your project's INSTALLED_APPs\n```python\nINSTALLED_APPS = (\n '....',\n 'ModelTracker',\n )\n``` \n* Add the following line to your urls.py\n```python\nimport ModelTracker\nurlpatterns = patterns('',\n...\nurl(r'^track/', include('ModelTracker.urls')),\n...\n)\n```\n* Run Migrations\n```sh\n python manage.py migrate ModelTracker \n```\n\n* Add the following line to your models.py file\n```python\nfrom ModelTracker import Tracker\n```\n* Convert each Model you want to track to inhert from `Tracker.ModelTracker` instead of `models.Model`\n \n**Old Code**\n\n```python\n class Employee(models.Model):\n name=models.CharField(max_length=255)\n address=models.CharField(max_length=255)\n age=models.IntegerField()\n ``` \n **New Code**\n \n ```python\n class Employee(Tracker.ModelTracker):\n name=models.CharField(max_length=255)\n address=models.CharField(max_length=255)\n age=models.IntegerField()\n```\n* For each save() call, add the user the username\n * Old Code\n ```python\n emp=Employee()\n emp.save()\n ``` \n \n * New Code\n ```python\n emp=Employee()\n emp.save(request.user.username)\n ```\n* Starting from version of 0.5, you can pass a event_name parameter to mark change as an event\n \n * New Code\n ```python\n emp=Employee()\n emp.save(request.user.username,event_name=\"Created the user\")\n ```\n\n# Using The Middleware\n\nYou can add `ModelTracker.middleware.ModelTrackerMiddleware` to your Middleware classes to get the username automatically from the request.\n\n```python\nMIDDLEWARE_CLASSES = (\n '....',\n 'ModelTracker.middleware.ModelTrackerMiddleware',\n )\n``` \n\n**Note:** If you pass username as `None` then the change won't be saved.\n\n# Showing Record History\n\nThere are 3 ways to see the history of a record\n 1. go to `ModelTracker` url and select `Table` and enter `id`.\n 2. call `showModelChanges` by POST and send `csrftokenmiddleware` to return history as html.\n 3. call `getModelChanges` which returns history as Json.\n\n# Django Admin\n\nThere is 2 ways to update an object by django admin\n1. Handle save and delete in ModelAdmin as follows\n ```python\n def save_model(self, request, obj, form, change):\n obj.save(request.user.username,\"Editing From admin interface\")\n\n def delete_model(self, request, obj):\n obj.delete(username=request.user.username, event_name=\"Deleting From admin interface\")\n ```\n2. Inhert from TrackerAdmin rather ModelAdmin\n ```python\n from ModelTracker.Tracker import TrackerAdmin \n admin.site.register(employee, TrackerAdmin)\n``` \n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Track Django Model Objects over time",
"version": "2.0.4",
"project_urls": {
"Download": "https://github.com/mkalioby/ModelTracker/",
"Homepage": "https://github.com/mkalioby/ModelTracker/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3d551c3a34615abc5474a65218a6b18884c3a8803dcaab12128c78efac4d7ab6",
"md5": "8a6a38143c091a771eb32fcca8a68d2c",
"sha256": "2da94b112b0ea4dd85b2a8e56bec21fddc28569cc1946e8a35215089ea62eba2"
},
"downloads": -1,
"filename": "django-model-tracker-2.0.4.tar.gz",
"has_sig": false,
"md5_digest": "8a6a38143c091a771eb32fcca8a68d2c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
"size": 61953,
"upload_time": "2023-07-25T05:38:51",
"upload_time_iso_8601": "2023-07-25T05:38:51.851671Z",
"url": "https://files.pythonhosted.org/packages/3d/55/1c3a34615abc5474a65218a6b18884c3a8803dcaab12128c78efac4d7ab6/django-model-tracker-2.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-25 05:38:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mkalioby",
"github_project": "ModelTracker",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-model-tracker"
}