Changeset 2510
- Timestamp:
- 03/11/06 21:58:11 (2 years ago)
- Files:
-
- django/branches/magic-removal/django/db/models/fields/related.py (modified) (5 diffs)
- django/branches/magic-removal/tests/modeltests/many_to_many/models.py (modified) (2 diffs)
- django/branches/magic-removal/tests/modeltests/many_to_one/models.py (modified) (1 diff)
- django/branches/magic-removal/tests/modeltests/many_to_one_null/models.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/db/models/fields/related.py
r2502 r2510 95 95 96 96 def __set__(self, instance, value): 97 if instance is None: 98 raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name 97 99 # Set the value of the related field 98 100 setattr(value, self.related.field.attname, instance) … … 127 129 128 130 def __set__(self, instance, value): 131 if instance is None: 132 raise AttributeError, "%s must be accessed via instance" % self._field.name 129 133 # Set the value of the related field 130 134 try: … … 198 202 return manager 199 203 204 def __set__(self, instance, value): 205 if instance is None: 206 raise AttributeError, "Manager must be accessed via instance" 207 208 manager = self.__get__(instance) 209 # If the foreign key can support nulls, then completely clear the related set. 210 # Otherwise, just move the named objects into the set. 211 if self.related.field.null: 212 manager.clear() 213 for obj in value: 214 manager.add(obj) 215 200 216 def _add_m2m_items(rel_manager_inst, managerclass, rel_model, join_table, source_col_name, 201 217 target_col_name, source_pk_val, *objs): … … 330 346 return manager 331 347 348 def __set__(self, instance, value): 349 if instance is None: 350 raise AttributeError, "Manager must be accessed via instance" 351 352 manager = self.__get__(instance) 353 manager.clear() 354 for obj in value: 355 manager.add(obj) 356 332 357 class ReverseManyRelatedObjectsDescriptor(object): 333 358 # This class provides the functionality that makes the related-object … … 401 426 402 427 return manager 428 429 def __set__(self, instance, value): 430 if instance is None: 431 raise AttributeError, "Manager must be accessed via instance" 432 433 manager = self.__get__(instance) 434 manager.clear() 435 for obj in value: 436 manager.add(obj) 403 437 404 438 class ForeignKey(RelatedField, Field): django/branches/magic-removal/tests/modeltests/many_to_many/models.py
r2409 r2510 145 145 [] 146 146 147 # You can clear the whole lot: 148 # (put some back first) 149 >>> p2.article_set.add(a4, a5) 150 >>> a4.publications.add(p3) 151 >>> a4.publications.all() 152 [Science News, Science Weekly] 147 # Relation sets can be assigned. Assignment clears any existing set members 148 >>> p2.article_set = [a4, a5] 149 >>> p2.article_set.all() 150 [NASA finds intelligent life on Earth, Oxygen-free diet works wonders] 151 >>> a4.publications.all() 152 [Science News] 153 >>> a4.publications = [p3] 154 >>> p2.article_set.all() 155 [Oxygen-free diet works wonders] 156 >>> a4.publications.all() 157 [Science Weekly] 158 159 # Relation sets can be cleared: 153 160 >>> p2.article_set.clear() 154 161 >>> p2.article_set.all() … … 197 204 [NASA uses Python] 198 205 199 200 206 """ django/branches/magic-removal/tests/modeltests/many_to_one/models.py
r2435 r2510 84 84 [] 85 85 86 # Set the article back again. 87 >>> new_article2.reporter = r2 88 >>> new_article2.save() 86 # Set the article back again using set descriptor. 87 >>> r2.article_set = [new_article, new_article2] 88 >>> r.article_set.all() 89 [This is a test] 90 >>> r2.article_set.all() 91 [John's second story, Paul's story] 92 93 # Funny case - assignment notation can only go so far; because the 94 # ForeignKey cannot be null, existing members of the set must remain 95 >>> r.article_set = [new_article] 96 >>> r.article_set.all() 97 [This is a test, John's second story] 98 >>> r2.article_set.all() 99 [Paul's story] 89 100 90 101 # Reporter cannot be null - there should not be a clear or remove method django/branches/magic-removal/tests/modeltests/many_to_one_null/models.py
r2434 r2510 50 50 51 51 # Reporter objects have access to their related Article objects. 52 >>> r.article_set. order_by('headline')52 >>> r.article_set.all() 53 53 [First, Second] 54 54 >>> r.article_set.filter(headline__startswith='Fir') … … 112 112 [Fourth] 113 113 114 # Use descriptor assignment to allocate ForeignKey. Null is legal, so 115 # existing members of set that are not in the assignment set are set null 116 >>> r2.article_set = [a2, a3] 117 >>> r2.article_set.all() 118 [Second, Third] 119 114 120 # Clear the rest of the set 115 121 >>> r.article_set.clear() … … 117 123 [] 118 124 >>> Article.objects.filter(reporter__isnull=True) 119 [First, Second, Third]125 [First, Fourth] 120 126 121 127 """
