Changeset 2435
- Timestamp:
- 02/28/06 05:36:20 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/db/models/fields/related.py
r2434 r2435 105 105 return rel_obj 106 106 107 def __set__(self, instance, value): 108 # Set the value of the related field 109 if value: 110 val = getattr(value, self._field.rel.get_related_field().attname) 111 else: 112 val = None 113 setattr(instance, self._field.attname, val) 114 115 # Set the cache to point to the new object 116 setattr(instance, self._field.get_cache_name(), value) 117 107 118 class ForeignRelatedObjectsDescriptor(object): 108 119 # This class provides the functionality that makes the related-object … … 131 142 def add(self, *objs): 132 143 for obj in objs: 133 val = getattr(instance, rel_field.rel.get_related_field().attname) 134 setattr(obj, rel_field.attname, val) 144 setattr(obj, rel_field.name, instance) 135 145 obj.save() 136 146 add.alters_data = True … … 147 157 val = getattr(instance, rel_field.rel.get_related_field().attname) 148 158 for obj in objs: 159 # Is obj actually part of this descriptor set? 149 160 if getattr(obj, rel_field.attname) == val: 150 setattr(obj, rel_field. attname, None)161 setattr(obj, rel_field.name, None) 151 162 obj.save() 152 163 else: … … 156 167 def clear(self): 157 168 for obj in self.all(): 158 setattr(obj, rel_field. attname, None)169 setattr(obj, rel_field.name, None) 159 170 obj.save() 160 171 add.alters_data = True django/branches/magic-removal/tests/modeltests/many_to_one/models.py
r2434 r2435 71 71 >>> r2.article_set.all() 72 72 [Paul's story] 73 74 # Assign the article to the reporter directly using the descriptor 75 >>> new_article2.reporter = r 76 >>> new_article2.save() 77 >>> new_article2.reporter 78 John Smith 79 >>> new_article2.reporter.id 80 1 81 >>> r.article_set.all() 82 [This is a test, John's second story, Paul's story] 83 >>> r2.article_set.all() 84 [] 85 86 # Set the article back again. 87 >>> new_article2.reporter = r2 88 >>> new_article2.save() 73 89 74 90 # Reporter cannot be null - there should not be a clear or remove method
