Ticket #3369: patch_1.diff

File patch_1.diff, 4.0 KB (added by wbyoung@…, 18 years ago)
  • django/db/models/manager.py

     
    101101
    102102    def select_related(self, *args, **kwargs):
    103103        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)
    104107
    105108    def values(self, *args, **kwargs):
    106109        return self.get_query_set().values(*args, **kwargs)
  • django/db/models/fields/related.py

     
    135135        # Set the value of the related field
    136136        setattr(value, self.related.field.rel.get_related_field().attname, instance)
    137137
    138         # Clear the cache, if it exists
     138        # Update the cache, if it exists
    139139        try:
    140             delattr(value, self.related.field.get_cache_name())
     140            setattr(value, self.related.field.get_cache_name(), value)
    141141        except AttributeError:
    142142            pass
    143143
     
    182182            val = None
    183183        setattr(instance, self.field.attname, val)
    184184
    185         # Clear the cache, if it exists
     185        # Update the cache, if it exists
    186186        try:
    187             delattr(instance, self.field.get_cache_name())
     187            setattr(instance, self.field.get_cache_name(), value)
    188188        except AttributeError:
    189189            pass
    190190
  • django/db/models/query.py

     
    9191        self._tables = []            # List of extra tables to use.
    9292        self._offset = None          # OFFSET clause.
    9393        self._limit = None           # LIMIT clause.
     94        self._auto_cache = {}        # Dictionary of items to automatically cache on the fetched objects.
    9495        self._result_cache = None
    9596
    9697    ########################
     
    192193                    obj = self.model(*row[:index_end])
    193194                for i, k in enumerate(extra_select):
    194195                    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])
    195199                yield obj
    196200
    197201    def count(self):
     
    386390    def distinct(self, true_or_false=True):
    387391        "Returns a new QuerySet instance with '_distinct' modified."
    388392        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)
    389397
    390398    def extra(self, select=None, where=None, params=None, tables=None):
    391399        assert self._limit is None and self._offset is None, \
     
    416424        c._tables = self._tables[:]
    417425        c._offset = self._offset
    418426        c._limit = self._limit
     427        c._auto_cache = self._auto_cache.copy()
    419428        c.__dict__.update(kwargs)
    420429        return c
    421430
  • tests/modeltests/many_to_one/models.py

     
    245245>>> Reporter.objects.filter(article__reporter=r).distinct()
    246246[<Reporter: John Smith>]
    247247
     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
     256True
     257
    248258# If you delete a reporter, his articles will be deleted.
    249259>>> Article.objects.all()
    250260[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>, <Article: This is a test>, <Article: This is a test>]
Back to Top