summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2017-08-04 11:20:15 +0500
committerTim Graham <timograham@gmail.com>2017-08-11 15:41:04 -0400
commit5cc746206726c538c36a2830e7c068f1c8a0e7c8 (patch)
treee172ff3a3a6325167f91704bf6aa5ac097be5435
parent97cb3bd16d8e50d96eb184f8d67dd1723f776d2a (diff)
Refs #28459 -- Optimized ModelState instantiation.
-rw-r--r--django/db/models/base.py22
-rw-r--r--tests/model_regress/test_state.py8
2 files changed, 23 insertions, 7 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 3cff2d2fc4..d39c0b9ea6 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -370,15 +370,23 @@ class ModelBase(type):
return cls._meta.default_manager
+class ModelStateFieldsCacheDescriptor:
+ def __get__(self, instance, cls=None):
+ if instance is None:
+ return self
+ res = instance.fields_cache = {}
+ return res
+
+
class ModelState:
"""Store model instance state."""
- def __init__(self, db=None):
- self.db = db
- # If true, uniqueness validation checks will consider this a new, as-yet-unsaved object.
- # Necessary for correct validation of new instances of objects with explicit (non-auto) PKs.
- # This impacts validation only; it has no effect on the actual save.
- self.adding = True
- self.fields_cache = {}
+ db = None
+ # If true, uniqueness validation checks will consider this a new, unsaved
+ # object. Necessary for correct validation of new instances of objects with
+ # explicit (non-auto) PKs. This impacts validation only; it has no effect
+ # on the actual save.
+ adding = True
+ fields_cache = ModelStateFieldsCacheDescriptor()
class Model(metaclass=ModelBase):
diff --git a/tests/model_regress/test_state.py b/tests/model_regress/test_state.py
new file mode 100644
index 0000000000..f41e4e52f4
--- /dev/null
+++ b/tests/model_regress/test_state.py
@@ -0,0 +1,8 @@
+from django.db.models.base import ModelState, ModelStateFieldsCacheDescriptor
+from django.test import SimpleTestCase
+
+
+class ModelStateTests(SimpleTestCase):
+
+ def test_fields_cache_descriptor(self):
+ self.assertIsInstance(ModelState.fields_cache, ModelStateFieldsCacheDescriptor)