Ticket #3369: patch_2.diff
File patch_2.diff, 5.5 KB (added by , 18 years ago) |
---|
-
django/db/models/manager.py
101 101 102 102 def select_related(self, *args, **kwargs): 103 103 return self.get_query_set().select_related(*args, **kwargs) 104 105 def auto_cache(self, *args, **kwargs): 106 return self.get_query_set().auto_cache(*args, **kwargs) 104 107 105 108 def values(self, *args, **kwargs): 106 109 return self.get_query_set().values(*args, **kwargs) -
django/db/models/fields/related.py
135 135 # Set the value of the related field 136 136 setattr(value, self.related.field.rel.get_related_field().attname, instance) 137 137 138 # Clearthe cache, if it exists138 # Update the cache, if it exists 139 139 try: 140 delattr(value, self.related.field.get_cache_name())140 setattr(value, self.related.field.get_cache_name(), value) 141 141 except AttributeError: 142 142 pass 143 143 … … 182 182 val = None 183 183 setattr(instance, self.field.attname, val) 184 184 185 # Clearthe cache, if it exists185 # Update the cache, if it exists 186 186 try: 187 delattr(instance, self.field.get_cache_name())187 setattr(instance, self.field.get_cache_name(), value) 188 188 except AttributeError: 189 189 pass 190 190 … … 210 210 211 211 class RelatedManager(superclass): 212 212 def get_query_set(self): 213 return superclass.get_query_set(self).filter(**(self.core_filters)) 213 return superclass.get_query_set(self).filter(**(self.core_filters)).auto_cache(**self.core_cache) 214 214 215 215 def add(self, *objs): 216 216 for obj in objs: … … 245 245 246 246 manager = RelatedManager() 247 247 manager.core_filters = {'%s__pk' % rel_field.name: getattr(instance, rel_field.rel.get_related_field().attname)} 248 manager.core_cache = { rel_field.name: instance } 248 249 manager.model = self.related.model 249 250 250 251 return manager -
django/db/models/query.py
91 91 self._tables = [] # List of extra tables to use. 92 92 self._offset = None # OFFSET clause. 93 93 self._limit = None # LIMIT clause. 94 self._auto_cache = {} # Dictionary of items to automatically cache on the fetched objects. 94 95 self._result_cache = None 95 96 96 97 ######################## … … 192 193 obj = self.model(*row[:index_end]) 193 194 for i, k in enumerate(extra_select): 194 195 setattr(obj, k[0], row[index_end+i]) 196 # auto cache items 197 for key in self._auto_cache: 198 setattr(obj, key, self._auto_cache[key]) 195 199 yield obj 196 200 197 201 def count(self): … … 386 390 def distinct(self, true_or_false=True): 387 391 "Returns a new QuerySet instance with '_distinct' modified." 388 392 return self._clone(_distinct=true_or_false) 393 394 def auto_cache(self, **kwargs): 395 """"Returns a new QuerySet instance with _auto_cache modified. 396 This is not available publicly. It is used to automatically cache 397 the reverse foreign key assocaiation when getting an object through 398 a FOO_set. """ 399 return self._clone(_auto_cache=kwargs) 389 400 390 401 def extra(self, select=None, where=None, params=None, tables=None): 391 402 assert self._limit is None and self._offset is None, \ … … 416 427 c._tables = self._tables[:] 417 428 c._offset = self._offset 418 429 c._limit = self._limit 430 c._auto_cache = self._auto_cache.copy() 419 431 c.__dict__.update(kwargs) 420 432 return c 421 433 -
tests/modeltests/many_to_one/models.py
245 245 >>> Reporter.objects.filter(article__reporter=r).distinct() 246 246 [<Reporter: John Smith>] 247 247 248 # Automatic caching 249 >>> r_ac = Reporter.objects.all()[0] 250 >>> r_ac 251 <Reporter: John Smith> 252 >>> a_ac = r_ac.article_set.all()[0] 253 >>> a_ac 254 <Article: John's second story> 255 >>> a_ac.reporter is r_ac 256 True 257 248 258 # If you delete a reporter, his articles will be deleted. 249 259 >>> Article.objects.all() 250 260 [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>, <Article: This is a test>, <Article: This is a test>] -
docs/db-api.txt
1446 1446 b.entry_set.filter(headline__contains='Lennon') 1447 1447 b.entry_set.count() 1448 1448 1449 Note that the reverse association from the foreign key is automatically cached 1450 when retrieving objects through this ``Manager``. 1451 1452 Example:: 1453 b = Blog.objects.get(id=1) 1454 for entry in b.entry_set.all(): 1455 entry.blog # No database access required. 1456 1449 1457 You can override the ``FOO_set`` name by setting the ``related_name`` 1450 1458 parameter in the ``ForeignKey()`` definition. For example, if the ``Entry`` 1451 1459 model was altered to ``blog = ForeignKey(Blog, related_name='entries')``, the