Last active
December 29, 2015 11:49
-
-
Save nickeddy/7666224 to your computer and use it in GitHub Desktop.
Append a copy of a model's data to self in __init__
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.db import models | |
from itertools import chain | |
class ModelName(models.Model): | |
def __init__(self, *args, **kwargs): | |
super(ModelName, self).__init__(*args, **kwargs) | |
if self.pk is not None: | |
field_names = [f.name for f in chain(self._meta.fields, self._meta.many_to_many)] | |
self._old = dict((fname, getattr(self, fname, None)) for fname in field_names) | |
else: | |
self._old = {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment