Skip to content

Commit

Permalink
Setting a reference field deletes the reference (and cached object)
Browse files Browse the repository at this point in the history
* fixes iamteem#3
  • Loading branch information
sebastien requiem committed Aug 20, 2012
1 parent 1553c5d commit 6b05392
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion redisco/models/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,21 @@ def __init__(self,
self.default = default

def __set__(self, instance, value):
"""
Will set the referenced object unless None is provided
which will simply remove the reference
"""
if not isinstance(value, self.value_type()) and \
value is not None:
raise TypeError
# remove the cached value from the instance
if hasattr(instance, '_' + self.name):
delattr(instance, '_' + self.name)
setattr(instance, self.attname, value.id)
# Remove the attribute_id reference
setattr(instance, self.attname, None)
# Set it to the new value if any.
if value is not None:
setattr(instance, self.attname, value.id)

def __get__(self, instance, owner):
try:
Expand Down

0 comments on commit 6b05392

Please sign in to comment.