Created
April 1, 2013 16:50
-
-
Save ebrelsford/5286132 to your computer and use it in GitHub Desktop.
A mixin for Django model managers that adds an initial revision using django-reversion when the object is created via get_or_create().
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
from django.utils.importlib import import_module | |
from reversion import default_revision_manager | |
class InitialRevisionManagerMixin(object): | |
def _save_initial_revision(self, obj): | |
# try to load admin | |
try: | |
import_module("%s.admin" % obj.__module__.rsplit('.', 1)[0]) | |
except ImportError: | |
pass | |
# save revision | |
default_revision_manager.save_revision((obj,), | |
comment='Initial revision') | |
def get_or_create(self, **kwargs): | |
obj, created = self.get_query_set().get_or_create(**kwargs) | |
if created: | |
self._save_initial_revision(obj) | |
return obj, created |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment