Package: django-trackman
Django Trackman helps with tracking and logging.FeaturesCustomizable models: You’ll have base models and an admin class mixins
that you can extend to define your own tracking models.Separate database: You can decide to use a separate database for your tracking data, as it could
grow fast. A database router will help you implement the Django multiple database feature.Loose coupling: The tracking table is not related to you application table with foreign keys.
This ensures that your tracking data is kept independent of your application data.Admin action tracking: You can enable tracking of Django admin actions.API Endpoint: You can define django-rest-framework views to create API endpoints. This can be
used to track actions made through API calls.Installationpipinstalldjango-trackmanAdd ‘trackman’ to your INSTALLED_APPS in Django’s settings:INSTALLED_APPS=(...'trackman',...)Then enable trackman by setting the variableTRACKMAN_ENABLEDtoTrue.Action Tracking ModelsDjango Trackman can be used to create custom tracking models.
Here’s an example of a model class that extends Django Trackman’sTrackingActionModel`:fromtrackman.modelsimportTrackingActionModelfromdjango.dbimportmodelsclassTrackingAction(TrackingActionModel):team=models.CharField("Team",max_length=256,blank=True)def__str__(self):returnf"Actor:{self.actor}, Action:{self.action}, Time:{self.created}"classMeta:verbose_name="Action Log"verbose_name_plural="Action Logs"How Django Trackman would know that this model should be used by default for tracking
your actions ? You’ll need to instruct it by defining a default tracking model in
your settings.TRACKMAN_MODELS={"default":"tracking.TrackingAction","data-quality":"tracking.DataQualityTracking",}Here, you’ll notice that we have also defined a additional tracking model for data
quality tracking.Base Tracking ModelsIf your models are not action oriented, you can always extend theTrackingBaseModelthat’s an empty abstract model that only serves as a way to let Trackman know that
your model should be consider as a tracking model and thus should be taken into account
when during database routing.AdminFor managing your tracking models in Django admin, Django Trackman provides the mix-in classTrackingModelAdminMixin.fromdjango.contribimportadminfromtrackman.adminimportTrackingModelAdminMixinfrom.modelsimportTrackingActionclassTrackingActionAdmin(TrackingModelAdminMixin,admin.ModelAdmin):list_display=["id","actor","team","action","object","target","description","created",]search_fields=["actor","team"]+TrackingModelAdminMixin.action_log_search_fieldslist_filter=["team","action"]ifsettings.TRACKMAN_ENABLED:admin.site.register(TrackingAction,TrackingActionAdmin)Tracking on a separate databaseWhen you want to isolate tracking data from your main application data for performance,
maintenance or data integrity reasons, you could route tracking database operations to a
separate database.Django’s multiple database feature allows you to use more than one database in your project.
It provides the flexibility to specify which models use which database - that’s defined by a
router. Django Trackman uses that feature to isolate you tracking data from your application
data.You will first need to instruct in your project’s settings, the database alias that should be
used.TRACKMAN_DATABASE_ALIAS="tracking"Then define the database accesses.DATABASES={"default":{'ENGINE':'django.db.backends.<app-database>','NAME':<app-database-name>,'USER':<-app-database-user>,'PASSWORD':<-app-database-password>,'HOST':<-app-database-host>,'PORT':<-app-database-port>,},}ifTRACKMAN_ENABLED:DATABASE_ROUTERS=["trackman.db_routers.TrackmanDatabaseRouter"]DATABASES[TRACKMAN_DATABASE_ALIAS]={'ENGINE':'django.db.backends.<tracking-db>','NAME':<tracking-database-name>,'USER':<tracking-database-user>,'PASSWORD':<tracking-database-password>,'HOST':<tracking-database-host>,'PORT':<tracking-database-port>,}If you are using a tool likedj-database-urlwith environnement variables:fromdjango.urlsimportdj_database_url.parseDATABASES={"default":dj_database_url.parse(env("APP_DATABASE_URL"))}ifTRACKMAN_ENABLED:DATABASE_ROUTERS=["trackman.db_routers.TrackingDatabaseRouter"]db_url=env("TRACKING_DATABASE_URL")DATABASES[TRACKMAN_DATABASE_ALIAS]=dj_database_url.parse(db_url)Tracking admin actionDjango Trackman, allows you to track Django admin actions - the actions that
the admin users are performing.Setting Up Django Trackman
The main file is trackman/signals.py, and it requires importing in some AppConfig’s
ready method.Let’s suppose that you have a backoffice app that you can use to setup signals.fromdjango.appsimportAppConfigimportsysclassBackofficeConfig(AppConfig):name="backoffice"verbose_name="Backoffice"defready(self):if"migrate"notinsys.argv:importtrackman.signals# noqaAs a consequence, all admin actions will be copied to you tracking table.API End-pointDjango Trackman provides a mixin you can use with Django Rest Framework’s ViewSet to create an API end-point
for your application to track actions. This could be useful for tracking front-end actions.Here’s how you can use TrackingViewSetMixin in a Django REST ViewSet for action tracking:fromrest_frameworkimportviewsetsfromtrackman.apiimportTrackingViewSetMixinclassActionTrackingViewSet(TrackingViewSetMixin,viewsets.ViewSet):model_alias="default"defclean_action_details(self,action_details):# Do some clean-up here...cleaned_data=action_details.copy()returncleaned_dataThemodel_aliaspoints out which Django Trackman model alias to be used for saving the tracking data.You’ll need to add this new ViewSet ActionTrackingViewSet to your url configuration to have it active.