Skip to content

Instantly share code, notes, and snippets.

@nickeddy
Last active December 29, 2015 11:49
Show Gist options
  • Save nickeddy/7666224 to your computer and use it in GitHub Desktop.
Save nickeddy/7666224 to your computer and use it in GitHub Desktop.
Append a copy of a model's data to self in __init__
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