Skip to content

Instantly share code, notes, and snippets.

@loic
Created June 4, 2014 06:52
Show Gist options
  • Save loic/57028547e85f7fea928e to your computer and use it in GitHub Desktop.
Save loic/57028547e85f7fea928e to your computer and use it in GitHub Desktop.
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 63da28d..9452b06 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -617,9 +617,12 @@ class ReverseSingleRelatedObjectDescriptor(object):
if related is not None:
setattr(related, self.field.related.get_cache_name(), None)
- # Set the value of the related field
- for lh_field, rh_field in self.field.related_fields:
- try:
+ for lh_field, rh_field in self.field.related_fields:
+ setattr(instance, lh_field.attname, None)
+
+ # Set the values of the related field.
+ else:
+ for lh_field, rh_field in self.field.related_fields:
val = getattr(value, rh_field.attname)
if val is None:
raise ValueError(
@@ -627,8 +630,6 @@ class ReverseSingleRelatedObjectDescriptor(object):
(value, self.field.rel.to._meta.object_name)
)
setattr(instance, lh_field.attname, val)
- except AttributeError:
- setattr(instance, lh_field.attname, None)
# Since we already know what the related object is, seed the related
# object caches now, too. This avoids another db hit if you get the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment