# Bazaar revision bundle v0.9
#
# message:
# update select_related tests with proper counts; basically heavy reduction of # of queries required via instance caching tricks in foreign key backref instances
# committer: Brian Harring <ferringb@gmail.com>
# date: Sun 2007-05-27 10:21:20.177999973 -0700
=== modified file django/core/serializers/python.py // last-changed:ferringb@gm
... ail.com-20070525002257-juow7h2cfizoox5u
|
|
|
89 | 89 | else: |
90 | 90 | data[field.name] = field.to_python(field_value) |
91 | 91 | |
| 92 | data["disable_inst_caching"] = True |
92 | 93 | yield base.DeserializedObject(Model(**data), m2m_data) |
93 | 94 | |
94 | 95 | def _get_model(model_identifier): |
=== modified file django/core/serializers/xml_serializer.py // last-changed:fer
... ringb@gmail.com-20070525001853-wnodu5zd8vdcda59
|
|
|
170 | 170 | value = field.to_python(getInnerText(field_node).strip().encode(self.encoding)) |
171 | 171 | data[field.name] = value |
172 | 172 | |
| 173 | data["disable_inst_caching"] = True |
173 | 174 | # Return a DeserializedObject so that the m2m data has a place to live. |
174 | 175 | return base.DeserializedObject(Model(**data), m2m_data) |
175 | 176 | |
… |
… |
|
226 | 227 | inner_text.extend(getInnerText(child)) |
227 | 228 | else: |
228 | 229 | pass |
229 | | return "".join(inner_text) |
230 | | No newline at end of file |
| 230 | return "".join(inner_text) |
=== modified file django/db/models/base.py // last-changed:ferringb@gmail.com-2
... 0070525001853-wnodu5zd8vdcda59
|
|
|
14 | 14 | from django.utils.functional import curry |
15 | 15 | from django.conf import settings |
16 | 16 | from itertools import izip |
| 17 | from weakref import WeakValueDictionary |
17 | 18 | import types |
18 | 19 | import sys |
19 | 20 | import os |
… |
… |
|
76 | 77 | # registered version. |
77 | 78 | return get_model(new_class._meta.app_label, name, False) |
78 | 79 | |
| 80 | def __call__(cls, *args, **kwargs): |
| 81 | if not kwargs.pop("disable_inst_caching", False) and cls._meta.has_auto_field: |
| 82 | key = cls._get_cache_key(args, kwargs) |
| 83 | if key is not None: |
| 84 | obj = cls.__instance_cache__.get(key) |
| 85 | if obj is None: |
| 86 | obj = super(ModelBase, cls).__call__(*args, **kwargs) |
| 87 | cls.__instance_cache__[key] = obj |
| 88 | else: |
| 89 | obj = super(ModelBase, cls).__call__(*args, **kwargs) |
| 90 | else: |
| 91 | obj = super(ModelBase, cls).__call__(*args, **kwargs) |
| 92 | return obj |
| 93 | |
| 94 | |
79 | 95 | class Model(object): |
80 | 96 | __metaclass__ = ModelBase |
81 | 97 | |
… |
… |
|
94 | 110 | def __ne__(self, other): |
95 | 111 | return not self.__eq__(other) |
96 | 112 | |
| 113 | def _get_cache_key(cls, args, kwargs): |
| 114 | # this should be calculated *once*, but isn't atm |
| 115 | pk_position = cls._meta.fields.index(cls._meta.pk) |
| 116 | if len(args) > pk_position: |
| 117 | return args[pk_position] |
| 118 | pk = cls._meta.pk |
| 119 | if pk.name in kwargs: |
| 120 | return kwargs[pk.name] |
| 121 | elif pk.attname in kwargs: |
| 122 | return kwargs[pk.attname] |
| 123 | return None |
| 124 | _get_cache_key = classmethod(_get_cache_key) |
| 125 | |
| 126 | def get_cached_instance(cls, id): |
| 127 | return cls.__instance_cache__.get(id) |
| 128 | get_cached_instance = classmethod(get_cached_instance) |
| 129 | |
97 | 130 | def __init__(self, *args, **kwargs): |
98 | 131 | dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs) |
99 | 132 | |
… |
… |
|
194 | 227 | if hasattr(cls, 'get_absolute_url'): |
195 | 228 | cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url) |
196 | 229 | |
| 230 | cls.__instance_cache__ = WeakValueDictionary() |
| 231 | |
197 | 232 | dispatcher.send(signal=signals.class_prepared, sender=cls) |
198 | 233 | |
199 | 234 | _prepare = classmethod(_prepare) |
… |
… |
|
251 | 286 | setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) |
252 | 287 | transaction.commit_unless_managed() |
253 | 288 | |
| 289 | # if we're a new instance that hasn't been written in; save ourself. |
| 290 | if self._meta.has_auto_field: |
| 291 | self.__instance_cache__[getattr(self, self._meta.pk.attname)] = self |
| 292 | |
254 | 293 | # Run any post-save hooks. |
255 | 294 | dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) |
256 | 295 | |
=== modified file django/db/models/fields/related.py // last-changed:ferringb@g
... mail.com-20070524150447-bxe88y4c8ixjsqqa
|
|
|
163 | 163 | if self.field.null: |
164 | 164 | return None |
165 | 165 | raise self.field.rel.to.DoesNotExist |
166 | | other_field = self.field.rel.get_related_field() |
167 | | if other_field.rel: |
168 | | params = {'%s__pk' % self.field.rel.field_name: val} |
169 | | else: |
170 | | params = {'%s__exact' % self.field.rel.field_name: val} |
171 | | rel_obj = self.field.rel.to._default_manager.get(**params) |
| 166 | rel_obj = self.field.rel.to.get_cached_instance(val) |
| 167 | if rel_obj is None: |
| 168 | other_field = self.field.rel.get_related_field() |
| 169 | if other_field.rel: |
| 170 | params = {'%s__pk' % self.field.rel.field_name: val} |
| 171 | else: |
| 172 | params = {'%s__exact' % self.field.rel.field_name: val} |
| 173 | rel_obj = self.field.rel.to._default_manager.get(**params) |
172 | 174 | setattr(instance, cache_name, rel_obj) |
173 | 175 | return rel_obj |
174 | 176 | |
=== modified file tests/modeltests/model_forms/models.py // last-changed:ferrin
... gb@gmail.com-20070525002908-88uk1p54t5xwec3w
|
|
|
244 | 244 | 1 |
245 | 245 | >>> test_art = Article.objects.get(id=1) |
246 | 246 | >>> test_art.headline |
247 | | 'Test headline' |
| 247 | u'Test headline' |
248 | 248 | |
249 | 249 | You can create a form over a subset of the available fields |
250 | 250 | by specifying a 'fields' argument to form_for_instance. |
… |
… |
|
260 | 260 | 1 |
261 | 261 | >>> new_art = Article.objects.get(id=1) |
262 | 262 | >>> new_art.headline |
263 | | 'New headline' |
| 263 | u'New headline' |
264 | 264 | |
265 | 265 | Add some categories and test the many-to-many form output. |
266 | 266 | >>> new_art.categories.all() |
=== modified file tests/modeltests/select_related/models.py
|
|
|
107 | 107 | 1 |
108 | 108 | |
109 | 109 | # select_related() also of course applies to entire lists, not just items. |
110 | | # Without select_related() |
| 110 | # Without select_related() (note instance caching still reduces this from 9 to 5) |
111 | 111 | >>> db.reset_queries() |
112 | 112 | >>> world = Species.objects.all() |
113 | 113 | >>> [o.genus.family for o in world] |
114 | 114 | [<Family: Drosophilidae>, <Family: Hominidae>, <Family: Fabaceae>, <Family: Amanitacae>] |
115 | 115 | >>> len(db.connection.queries) |
116 | | 9 |
| 116 | 5 |
117 | 117 | |
118 | 118 | # With select_related(): |
119 | 119 | >>> db.reset_queries() |
… |
… |
|
129 | 129 | >>> pea.genus.family.order.klass.phylum.kingdom.domain |
130 | 130 | <Domain: Eukaryota> |
131 | 131 | |
132 | | # Notice: one few query than above because of depth=1 |
| 132 | # notice: instance caching saves the day; would be 7 without. |
133 | 133 | >>> len(db.connection.queries) |
134 | | 7 |
| 134 | 1 |
135 | 135 | |
136 | 136 | >>> db.reset_queries() |
137 | 137 | >>> pea = Species.objects.select_related(depth=5).get(name="sativum") |
138 | 138 | >>> pea.genus.family.order.klass.phylum.kingdom.domain |
139 | 139 | <Domain: Eukaryota> |
140 | 140 | >>> len(db.connection.queries) |
141 | | 3 |
| 141 | 1 |
142 | 142 | |
143 | 143 | >>> db.reset_queries() |
144 | 144 | >>> world = Species.objects.all().select_related(depth=2) |
145 | 145 | >>> [o.genus.family.order for o in world] |
146 | 146 | [<Order: Diptera>, <Order: Primates>, <Order: Fabales>, <Order: Agaricales>] |
147 | 147 | >>> len(db.connection.queries) |
148 | | 5 |
| 148 | 1 |
149 | 149 | |
150 | 150 | # Reset DEBUG to where we found it. |
151 | 151 | >>> settings.DEBUG = False |