From d4a6375fba108b2319f2dbad474c8bfc3b6adb5b Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 30 Mar 2006 13:12:11 +0000 Subject: [PATCH] magic-removal: Modified caching strategy for descriptors to force re-evaluation of cache on the first get following a set. This is required to avoid duplication of the 'should I throw DoesNotExist' logic. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2598 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 2b0dc13a55..e2601c7544 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -99,8 +99,11 @@ class SingleRelatedObjectDescriptor(object): # Set the value of the related field setattr(value, self.related.field.rel.get_related_field().attname, instance) - # Set the cache on the provided object to point to the new object - setattr(value, self.related.field.get_cache_name(), instance) + # Clear the cache, if it exists + try: + delattr(value, self.related.field.get_cache_name()) + except AttributeError: + pass class ReverseSingleRelatedObjectDescriptor(object): # This class provides the functionality that makes the related-object @@ -143,8 +146,11 @@ class ReverseSingleRelatedObjectDescriptor(object): val = None setattr(instance, self.field.attname, val) - # Set the cache to point to the new object - setattr(instance, self.field.get_cache_name(), value) + # Clear the cache, if it exists + try: + delattr(instance, self.field.get_cache_name()) + except AttributeError: + pass class ForeignRelatedObjectsDescriptor(object): # This class provides the functionality that makes the related-object