1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #24591 -- Optimized cloning of ModelState objects.

Changed ModelState.clone() to create a shallow copy of self.fields
and self.managers.
This commit is contained in:
Marten Kenbeek
2015-04-06 20:17:13 +02:00
committed by Tim Graham
parent c331eeb89c
commit 1a1f16d67d
3 changed files with 42 additions and 4 deletions

View File

@@ -312,12 +312,23 @@ class ModelState(object):
# Sanity-check that fields is NOT a dict. It must be ordered.
if isinstance(self.fields, dict):
raise ValueError("ModelState.fields cannot be a dict - it must be a list of 2-tuples.")
# Sanity-check that fields are NOT already bound to a model.
for name, field in fields:
# Sanity-check that fields are NOT already bound to a model.
if hasattr(field, 'model'):
raise ValueError(
'ModelState.fields cannot be bound to a model - "%s" is.' % name
)
# Sanity-check that relation fields are NOT referring to a model class.
if field.is_relation and hasattr(field.related_model, '_meta'):
raise ValueError(
'ModelState.fields cannot refer to a model class - "%s.to" does. '
'Use a string reference instead.' % name
)
if field.many_to_many and hasattr(field.remote_field.through, '_meta'):
raise ValueError(
'ModelState.fields cannot refer to a model class - "%s.through" does. '
'Use a string reference instead.' % name
)
@cached_property
def name_lower(self):
@@ -502,10 +513,10 @@ class ModelState(object):
return self.__class__(
app_label=self.app_label,
name=self.name,
fields=list(self.construct_fields()),
fields=list(self.fields),
options=dict(self.options),
bases=self.bases,
managers=list(self.construct_managers()),
managers=list(self.managers),
)
def render(self, apps):