Changeset 2434
- Timestamp:
- 02/28/06 04:12:45 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/db/models/fields/related.py
r2409 r2434 142 142 create.alters_data = True 143 143 144 # remove() and clear() are only provided if the ForeignKey can have a value of null. 145 if rel_field.null: 146 def remove(self, *objs): 147 val = getattr(instance, rel_field.rel.get_related_field().attname) 148 for obj in objs: 149 if getattr(obj, rel_field.attname) == val: 150 setattr(obj, rel_field.attname, None) 151 obj.save() 152 else: 153 raise rel_field.rel.to.DoesNotExist, "'%s' is not related to '%s'." % (obj, instance) 154 add.alters_data = True 155 156 def clear(self): 157 for obj in self.all(): 158 setattr(obj, rel_field.attname, None) 159 obj.save() 160 add.alters_data = True 161 144 162 manager = RelatedManager() 145 163 manager.core_filters = {'%s__pk' % rel_field.name: getattr(instance, rel_field.rel.get_related_field().attname)} django/branches/magic-removal/tests/modeltests/many_to_one/models.py
r2409 r2434 71 71 >>> r2.article_set.all() 72 72 [Paul's story] 73 74 # Reporter cannot be null - there should not be a clear or remove method 75 >>> hasattr(r2.article_set, 'remove') 76 False 77 >>> hasattr(r2.article_set, 'clear') 78 False 73 79 74 80 # Reporter objects have access to their related Article objects. django/branches/magic-removal/tests/modeltests/many_to_one_null/models.py
r2409 r2434 21 21 return self.headline 22 22 23 class Meta: 24 ordering = ('headline',) 25 23 26 API_TESTS = """ 24 27 # Create a Reporter. … … 80 83 >>> Article.objects.filter(reporter__isnull=True) 81 84 [Third] 85 86 # Set the reporter for the Third article 87 >>> r.article_set.add(a3) 88 >>> r.article_set.all() 89 [First, Second, Third] 90 91 # Remove an article from the set, and check that it was removed. 92 >>> r.article_set.remove(a3) 93 >>> r.article_set.all() 94 [First, Second] 95 >>> Article.objects.filter(reporter__isnull=True) 96 [Third] 97 98 # Create another article and reporter 99 >>> r2 = Reporter(name='Paul Jones') 100 >>> r2.save() 101 >>> a4 = r2.article_set.create(headline='Fourth') 102 >>> r2.article_set.all() 103 [Fourth] 104 105 # Try to remove a4 from a set it does not belong to 106 >>> r.article_set.remove(a4) 107 Traceback (most recent call last): 108 ... 109 DoesNotExist: 'Fourth' is not related to 'John Smith'. 110 111 >>> r2.article_set.all() 112 [Fourth] 113 114 # Clear the rest of the set 115 >>> r.article_set.clear() 116 >>> r.article_set.all() 117 [] 118 >>> Article.objects.filter(reporter__isnull=True) 119 [First, Second, Third] 120 82 121 """
