Skip to content

Instantly share code, notes, and snippets.

@a-leut
Created August 8, 2017 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-leut/7b37f1ea258d3f24f7598cffe0ce6b10 to your computer and use it in GitHub Desktop.
Save a-leut/7b37f1ea258d3f24f7598cffe0ce6b10 to your computer and use it in GitHub Desktop.
adds a version property to models implementing django-simple-history
class HistoryHelperMixin(object):
""" Adds a version property to models implementing django-simple-history
https://github.com/treyhunner/django-simple-history
"""
@property
def version(self):
return self.__class__._get_history_id(self.id)
@classmethod
def _get_history_id(cls, id):
""" Gets the most recent history id column for the given class
"""
if not getattr(cls, 'history', False):
raise ValueError('Class does not have a history field')
second_most_recent = cls.history.filter(id=id).order_by('-history_id').first()
return second_most_recent.history_id + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment