Ticket #3369: patch_1.diff
File patch_1.diff, 4.0 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 -
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 return self._clone(_auto_cache=kwargs) 389 397 390 398 def extra(self, select=None, where=None, params=None, tables=None): 391 399 assert self._limit is None and self._offset is None, \ … … 416 424 c._tables = self._tables[:] 417 425 c._offset = self._offset 418 426 c._limit = self._limit 427 c._auto_cache = self._auto_cache.copy() 419 428 c.__dict__.update(kwargs) 420 429 return c 421 430 -
tests/modeltests/many_to_one/models.py
245 245 >>> Reporter.objects.filter(article__reporter=r).distinct() 246 246 [<Reporter: John Smith>] 247 247 248 # Auto caching 249 >>> r_autocache = Reporter.objects.all()[0] 250 >>> r_autocache 251 <Reporter: John Smith> 252 >>> a_autocache = r_autocache.article_set.auto_cache(reporter=r_autocache)[0] 253 >>> a_autocache 254 <Article: John's second story> 255 >>> a_autocache.reporter is r_autocache 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>]