mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Refs #28459 -- Optimized ModelState instantiation.
This commit is contained in:
parent
97cb3bd16d
commit
5cc7462067
@ -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):
|
||||
|
8
tests/model_regress/test_state.py
Normal file
8
tests/model_regress/test_state.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user