From 018b3b5d8a2ac67a7ad494765ac838b386d8d678 Mon Sep 17 00:00:00 2001 From: Evan Heidtmann Date: Wed, 20 May 2015 10:47:20 -0700 Subject: [PATCH] Don't raise errors when loading old objects from fixtures This seems to work with a VersionedManyToManyField, but needs more testing. --- versions/models.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/versions/models.py b/versions/models.py index 2781f98..bdd5dba 100644 --- a/versions/models.py +++ b/versions/models.py @@ -46,6 +46,13 @@ def get_utc_now(): return datetime.datetime.utcnow().replace(tzinfo=utc) +def running_in_deserializer(): + import traceback + for path, line_no, function_name, code_line in reversed(traceback.extract_stack()): + if path.endswith('django/core/serializers/base.py'): + return True + + return False QueryTime = namedtuple('QueryTime', 'time active') @@ -865,7 +872,7 @@ def _remove_items_at(self, timestamp, source_field_name, target_field_name, *obj if 'add' in dir(many_related_manager_klass): def add(self, *objs): - if not self.instance.is_current: + if not self.instance.is_current and not running_in_deserializer(): raise SuspiciousOperation( "Adding many-to-many related objects is only possible on the current version") @@ -958,7 +965,7 @@ def __set__(self, instance, value): :param value: iterable of items to set """ - if not instance.is_current: + if not instance.is_current and not running_in_deserializer(): raise SuspiciousOperation( "Related values can only be directly set on the current version of an object")