Created
August 8, 2017 22:23
-
-
Save a-leut/7b37f1ea258d3f24f7598cffe0ce6b10 to your computer and use it in GitHub Desktop.
adds a version property to models implementing django-simple-history
This file contains 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
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