=== modified file 'django/core/serializers/python.py'
|
|
|
94 | 94 | else: |
95 | 95 | data[field.name] = field.to_python(field_value) |
96 | 96 | |
| 97 | data["disable_inst_caching"] = True |
97 | 98 | yield base.DeserializedObject(Model(**data), m2m_data) |
98 | 99 | |
99 | 100 | def _get_model(model_identifier): |
=== modified file 'django/core/serializers/xml_serializer.py'
|
|
|
176 | 176 | value = field.to_python(getInnerText(field_node).strip().encode(self.encoding)) |
177 | 177 | data[field.name] = value |
178 | 178 | |
| 179 | data["disable_inst_caching"] = True |
179 | 180 | # Return a DeserializedObject so that the m2m data has a place to live. |
180 | 181 | return base.DeserializedObject(Model(**data), m2m_data) |
181 | 182 | |
… |
… |
|
232 | 233 | inner_text.extend(getInnerText(child)) |
233 | 234 | else: |
234 | 235 | pass |
235 | | return "".join(inner_text) |
236 | | No newline at end of file |
| 236 | return "".join(inner_text) |
=== modified file 'django/db/models/base.py'
|
|
|
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__[self._get_pk_val()] = self |
| 292 | |
254 | 293 | # Run any post-save hooks. |
255 | 294 | dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) |
256 | 295 | |
… |
… |
|
311 | 350 | self._collect_sub_objects(seen_objs) |
312 | 351 | |
313 | 352 | # Actually delete the objects |
| 353 | if self._meta.has_auto_field: |
| 354 | pk = self._get_pk_val() |
| 355 | if pk is not None: |
| 356 | self.__instance_cache__.pop(pk, None) |
314 | 357 | delete_objects(seen_objs) |
315 | 358 | |
316 | 359 | delete.alters_data = True |
=== modified file 'django/db/models/fields/related.py'
|
|
|
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 'django/db/models/query.py'
|
|
|
1048 | 1048 | for cls in ordered_classes: |
1049 | 1049 | seen_objs[cls] = seen_objs[cls].items() |
1050 | 1050 | seen_objs[cls].sort() |
| 1051 | clean_inst_cache = cls.__instance_cache__.pop |
1051 | 1052 | |
1052 | 1053 | # Pre notify all instances to be deleted |
1053 | 1054 | for pk_val, instance in seen_objs[cls]: |
1054 | 1055 | dispatcher.send(signal=signals.pre_delete, sender=cls, instance=instance) |
1055 | 1056 | |
1056 | 1057 | pk_list = [pk for pk,instance in seen_objs[cls]] |
| 1058 | # we wipe the cache now; it's *possible* some form of a __get__ lookup may reintroduce an item after |
| 1059 | # the fact with the same pk (extremely unlikely) |
| 1060 | for x in pk_list: |
| 1061 | clean_inst_cache(x, None) |
| 1062 | |
1057 | 1063 | for related in cls._meta.get_all_related_many_to_many_objects(): |
1058 | 1064 | if not isinstance(related.field, generic.GenericRelation): |
1059 | | for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
| 1065 | for offset in xrange(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
1060 | 1066 | cursor.execute("DELETE FROM %s WHERE %s IN (%s)" % \ |
1061 | 1067 | (qn(related.field.m2m_db_table()), |
1062 | 1068 | qn(related.field.m2m_reverse_name()), |
1063 | | ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), |
1064 | | pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]) |
| 1069 | ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), |
| 1070 | pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]) |
| 1071 | |
1065 | 1072 | for f in cls._meta.many_to_many: |
1066 | 1073 | if isinstance(f, generic.GenericRelation): |
1067 | 1074 | from django.contrib.contenttypes.models import ContentType |
… |
… |
|
1070 | 1077 | else: |
1071 | 1078 | query_extra = '' |
1072 | 1079 | args_extra = [] |
1073 | | for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
| 1080 | for offset in xrange(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
1074 | 1081 | cursor.execute(("DELETE FROM %s WHERE %s IN (%s)" % \ |
1075 | 1082 | (qn(f.m2m_db_table()), qn(f.m2m_column_name()), |
1076 | 1083 | ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]]))) + query_extra, |
1077 | 1084 | pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE] + args_extra) |
| 1085 | |
1078 | 1086 | for field in cls._meta.fields: |
1079 | 1087 | if field.rel and field.null and field.rel.to in seen_objs: |
1080 | | for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
| 1088 | for offset in xrange(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
1081 | 1089 | cursor.execute("UPDATE %s SET %s=NULL WHERE %s IN (%s)" % \ |
1082 | 1090 | (qn(cls._meta.db_table), qn(field.column), qn(cls._meta.pk.column), |
1083 | 1091 | ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), |
… |
… |
|
1087 | 1095 | for cls in ordered_classes: |
1088 | 1096 | seen_objs[cls].reverse() |
1089 | 1097 | pk_list = [pk for pk,instance in seen_objs[cls]] |
1090 | | for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
| 1098 | clean_inst_cache = cls.__instance_cache__.pop |
| 1099 | for x in pk_list: |
| 1100 | clean_inst_cache(x, None) |
| 1101 | for offset in xrange(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
1091 | 1102 | cursor.execute("DELETE FROM %s WHERE %s IN (%s)" % \ |
1092 | 1103 | (qn(cls._meta.db_table), qn(cls._meta.pk.column), |
1093 | 1104 | ','.join(['%s' for pk in pk_list[offset:offset+GET_ITERATOR_CHUNK_SIZE]])), |
=== modified file 'tests/modeltests/model_forms/models.py'
|
|
|
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 |