is_dirty and dict of changed values

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class DiffingMixin(object):
	def __init__(self, *args, **kwargs):
		  super(DiffingMixin, self).__init__(*args, **kwargs)
		  self._original_state = dict(self.__dict__)
	def save(self, *args, **kwargs):
		state = dict(self.__dict__)
		del state['_original_state']
		self._original_state = state
		super(DiffingMixin, self).save()
	def is_dirty(self):
		missing = object()
		result = {}
		for key, value in self._original_state.iteritems():
		  if value != self.__dict__.get(key, missing):
				return True
		return False
	def changed_columns(self):
		missing = object()
		result = {}
		for key, value in self._original_state.iteritems():
		  if value != self.__dict__.get(key, missing):
				result[key] = {'old':value, 'new':self.__dict__.get(key, missing)}
		return result

More like this

  1. db_dump.py - for dumpping and loading data from database by limodou 4 years, 11 months ago
  2. Trigger a user password change by jedie 4 years, 5 months ago
  3. State Machine inspired by acts_as_state_machine by santuri 3 years, 9 months ago
  4. Django Dictionary Model by Morgul 8 months, 1 week ago
  5. HTML to POST dict converter by guettli 4 years, 2 months ago

Comments

(Forgotten your password?)