Kaydet (Commit) 5cc74620 authored tarafından Sergey Fedoseev's avatar Sergey Fedoseev Kaydeden (comit) Tim Graham

Refs #28459 -- Optimized ModelState instantiation.

üst 97cb3bd1
...@@ -370,15 +370,23 @@ class ModelBase(type): ...@@ -370,15 +370,23 @@ class ModelBase(type):
return cls._meta.default_manager 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: class ModelState:
"""Store model instance state.""" """Store model instance state."""
def __init__(self, db=None): db = None
self.db = db # If true, uniqueness validation checks will consider this a new, unsaved
# If true, uniqueness validation checks will consider this a new, as-yet-unsaved object. # object. Necessary for correct validation of new instances of objects with
# Necessary for correct validation of new instances of objects with explicit (non-auto) PKs. # explicit (non-auto) PKs. This impacts validation only; it has no effect
# This impacts validation only; it has no effect on the actual save. # on the actual save.
self.adding = True adding = True
self.fields_cache = {} fields_cache = ModelStateFieldsCacheDescriptor()
class Model(metaclass=ModelBase): class Model(metaclass=ModelBase):
......
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment