Api Versioning With Django Admin
> Home

Django admin versioning

A client required a web app that allowed for them to input data which was then served API style back to a mobile app.
The requirements for the project included a method of versioning the response to allow clients of the service to automatically determine if the response had changed in the case they locally cached the data. As the data input occurred only via the Django admin, logs are automatically created any time objects are changed and the easiest way to create a versioning system was to simply use the timestamp on these log objects to create a version number.

from django.contrib.admin.models import LogEntry
import pendulum

version = pendulum.instance(LogEntry.objects.order_by('action_time').last().action_time).format('YYYYMMDDHHmmss', formatter='alternative')

When output within the JSON documents, this provides per second level accuracy, which is likely far more than required, and is rendered as

"version": "20180326035947",

Whether or not having the client utilise the Django admin to enter and edit the data was a wise design decision is a discussion best left for another day.

Contents