2014-11-06 03:53:39 +08:00
|
|
|
import copy
|
2015-01-28 20:35:27 +08:00
|
|
|
from collections import OrderedDict
|
2017-09-07 20:16:21 +08:00
|
|
|
from contextlib import contextmanager
|
2014-05-06 01:50:51 +08:00
|
|
|
|
2013-12-29 03:13:08 +08:00
|
|
|
from django.apps import AppConfig
|
2014-05-01 03:25:12 +08:00
|
|
|
from django.apps.registry import Apps, apps as global_apps
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.conf import settings
|
2013-05-11 00:07:13 +08:00
|
|
|
from django.db import models
|
2014-06-16 05:55:44 +08:00
|
|
|
from django.db.models.fields.proxy import OrderWrt
|
2015-03-03 16:43:56 +08:00
|
|
|
from django.db.models.fields.related import RECURSIVE_RELATIONSHIP_CONSTANT
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.options import DEFAULT_NAMES, normalize_together
|
2015-03-03 16:43:56 +08:00
|
|
|
from django.db.models.utils import make_model_tuple
|
2014-11-05 17:43:31 +08:00
|
|
|
from django.utils.functional import cached_property
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2014-12-25 20:30:37 +08:00
|
|
|
from django.utils.version import get_docs_version
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2015-05-02 02:46:07 +08:00
|
|
|
from .exceptions import InvalidBasesError
|
2013-09-07 01:14:09 +08:00
|
|
|
|
|
|
|
|
2015-02-09 08:11:25 +08:00
|
|
|
def _get_app_label_and_model_name(model, app_label=''):
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(model, str):
|
2015-02-09 08:11:25 +08:00
|
|
|
split = model.split('.', 1)
|
2017-09-11 03:26:06 +08:00
|
|
|
return tuple(split) if len(split) == 2 else (app_label, split[0])
|
2015-02-09 08:11:25 +08:00
|
|
|
else:
|
|
|
|
return model._meta.app_label, model._meta.model_name
|
|
|
|
|
|
|
|
|
2015-10-20 06:40:25 +08:00
|
|
|
def _get_related_models(m):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return all models that have a direct relationship to the given model."""
|
2015-10-20 06:40:25 +08:00
|
|
|
related_models = [
|
|
|
|
subclass for subclass in m.__subclasses__()
|
|
|
|
if issubclass(subclass, models.Model)
|
|
|
|
]
|
|
|
|
related_fields_models = set()
|
|
|
|
for f in m._meta.get_fields(include_parents=True, include_hidden=True):
|
2016-12-29 23:27:49 +08:00
|
|
|
if f.is_relation and f.related_model is not None and not isinstance(f.related_model, str):
|
2015-10-20 06:40:25 +08:00
|
|
|
related_fields_models.add(f.model)
|
|
|
|
related_models.append(f.related_model)
|
|
|
|
# Reverse accessors of foreign keys to proxy models are attached to their
|
|
|
|
# concrete proxied model.
|
|
|
|
opts = m._meta
|
|
|
|
if opts.proxy and m in related_fields_models:
|
|
|
|
related_models.append(opts.concrete_model)
|
|
|
|
return related_models
|
|
|
|
|
|
|
|
|
2016-11-06 19:03:05 +08:00
|
|
|
def get_related_models_tuples(model):
|
|
|
|
"""
|
|
|
|
Return a list of typical (app_label, model_name) tuples for all related
|
|
|
|
models for the given model.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
(rel_mod._meta.app_label, rel_mod._meta.model_name)
|
|
|
|
for rel_mod in _get_related_models(model)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 08:11:25 +08:00
|
|
|
def get_related_models_recursive(model):
|
|
|
|
"""
|
2015-10-20 06:40:25 +08:00
|
|
|
Return all models that have a direct or indirect relationship
|
2015-02-09 08:11:25 +08:00
|
|
|
to the given model.
|
2015-04-01 05:35:41 +08:00
|
|
|
|
|
|
|
Relationships are either defined by explicit relational fields, like
|
|
|
|
ForeignKey, ManyToManyField or OneToOneField, or by inheriting from another
|
|
|
|
model (a superclass is related to its subclasses, but not vice versa). Note,
|
|
|
|
however, that a model inheriting from a concrete model is also related to
|
|
|
|
its superclass through the implicit *_ptr OneToOneField on the subclass.
|
2015-02-09 08:11:25 +08:00
|
|
|
"""
|
|
|
|
seen = set()
|
2015-10-20 06:40:25 +08:00
|
|
|
queue = _get_related_models(model)
|
2015-02-09 08:11:25 +08:00
|
|
|
for rel_mod in queue:
|
|
|
|
rel_app_label, rel_model_name = rel_mod._meta.app_label, rel_mod._meta.model_name
|
|
|
|
if (rel_app_label, rel_model_name) in seen:
|
|
|
|
continue
|
|
|
|
seen.add((rel_app_label, rel_model_name))
|
2015-10-20 06:40:25 +08:00
|
|
|
queue.extend(_get_related_models(rel_mod))
|
2015-02-09 08:11:25 +08:00
|
|
|
return seen - {(model._meta.app_label, model._meta.model_name)}
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ProjectState:
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Represent the entire project's overall state. This is the item that is
|
|
|
|
passed around - do it here rather than at the app level so that cross-app
|
|
|
|
FKs/etc. resolve properly.
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
|
|
|
|
2014-05-01 03:25:12 +08:00
|
|
|
def __init__(self, models=None, real_apps=None):
|
2013-05-11 00:07:13 +08:00
|
|
|
self.models = models or {}
|
2014-05-01 03:25:12 +08:00
|
|
|
# Apps to include from main registry, usually unmigrated ones
|
|
|
|
self.real_apps = real_apps or []
|
2016-11-06 19:03:05 +08:00
|
|
|
self.is_delayed = False
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2014-11-06 03:53:39 +08:00
|
|
|
def add_model(self, model_state):
|
2015-01-02 23:37:21 +08:00
|
|
|
app_label, model_name = model_state.app_label, model_state.name_lower
|
2014-11-06 03:53:39 +08:00
|
|
|
self.models[(app_label, model_name)] = model_state
|
|
|
|
if 'apps' in self.__dict__: # hasattr would cache the property
|
|
|
|
self.reload_model(app_label, model_name)
|
|
|
|
|
|
|
|
def remove_model(self, app_label, model_name):
|
|
|
|
del self.models[app_label, model_name]
|
|
|
|
if 'apps' in self.__dict__: # hasattr would cache the property
|
|
|
|
self.apps.unregister_model(app_label, model_name)
|
2015-04-04 05:36:35 +08:00
|
|
|
# Need to do this explicitly since unregister_model() doesn't clear
|
|
|
|
# the cache automatically (#24513)
|
|
|
|
self.apps.clear_cache()
|
2014-11-06 03:53:39 +08:00
|
|
|
|
2016-11-06 20:20:08 +08:00
|
|
|
def _find_reload_model(self, app_label, model_name, delay=False):
|
2016-11-06 19:03:05 +08:00
|
|
|
if delay:
|
|
|
|
self.is_delayed = True
|
|
|
|
|
2016-11-06 20:20:08 +08:00
|
|
|
related_models = set()
|
|
|
|
|
|
|
|
try:
|
|
|
|
old_model = self.apps.get_model(app_label, model_name)
|
|
|
|
except LookupError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Get all relations to and from the old model before reloading,
|
|
|
|
# as _meta.apps may change
|
|
|
|
if delay:
|
|
|
|
related_models = get_related_models_tuples(old_model)
|
|
|
|
else:
|
|
|
|
related_models = get_related_models_recursive(old_model)
|
|
|
|
|
|
|
|
# Get all outgoing references from the model to be rendered
|
|
|
|
model_state = self.models[(app_label, model_name)]
|
|
|
|
# Directly related models are the models pointed to by ForeignKeys,
|
|
|
|
# OneToOneFields, and ManyToManyFields.
|
|
|
|
direct_related_models = set()
|
|
|
|
for name, field in model_state.fields:
|
|
|
|
if field.is_relation:
|
|
|
|
if field.remote_field.model == RECURSIVE_RELATIONSHIP_CONSTANT:
|
|
|
|
continue
|
|
|
|
rel_app_label, rel_model_name = _get_app_label_and_model_name(field.related_model, app_label)
|
|
|
|
direct_related_models.add((rel_app_label, rel_model_name.lower()))
|
|
|
|
|
|
|
|
# For all direct related models recursively get all related models.
|
|
|
|
related_models.update(direct_related_models)
|
|
|
|
for rel_app_label, rel_model_name in direct_related_models:
|
2014-11-06 03:53:39 +08:00
|
|
|
try:
|
2016-11-06 20:20:08 +08:00
|
|
|
rel_model = self.apps.get_model(rel_app_label, rel_model_name)
|
2014-11-06 03:53:39 +08:00
|
|
|
except LookupError:
|
2016-11-06 20:20:08 +08:00
|
|
|
pass
|
2015-02-09 08:11:25 +08:00
|
|
|
else:
|
2016-11-06 19:03:05 +08:00
|
|
|
if delay:
|
2016-11-06 20:20:08 +08:00
|
|
|
related_models.update(get_related_models_tuples(rel_model))
|
2015-04-04 00:42:44 +08:00
|
|
|
else:
|
2016-11-06 20:20:08 +08:00
|
|
|
related_models.update(get_related_models_recursive(rel_model))
|
|
|
|
|
|
|
|
# Include the model itself
|
|
|
|
related_models.add((app_label, model_name))
|
|
|
|
|
|
|
|
return related_models
|
|
|
|
|
|
|
|
def reload_model(self, app_label, model_name, delay=False):
|
|
|
|
if 'apps' in self.__dict__: # hasattr would cache the property
|
|
|
|
related_models = self._find_reload_model(app_label, model_name, delay)
|
|
|
|
self._reload(related_models)
|
|
|
|
|
|
|
|
def reload_models(self, models, delay=True):
|
|
|
|
if 'apps' in self.__dict__: # hasattr would cache the property
|
|
|
|
related_models = set()
|
|
|
|
for app_label, model_name in models:
|
|
|
|
related_models.update(self._find_reload_model(app_label, model_name, delay))
|
|
|
|
self._reload(related_models)
|
|
|
|
|
|
|
|
def _reload(self, related_models):
|
|
|
|
# Unregister all related models
|
|
|
|
with self.apps.bulk_update():
|
2015-02-09 08:11:25 +08:00
|
|
|
for rel_app_label, rel_model_name in related_models:
|
2016-11-06 20:20:08 +08:00
|
|
|
self.apps.unregister_model(rel_app_label, rel_model_name)
|
|
|
|
|
|
|
|
states_to_be_rendered = []
|
|
|
|
# Gather all models states of those models that will be rerendered.
|
|
|
|
# This includes:
|
|
|
|
# 1. All related models of unmigrated apps
|
|
|
|
for model_state in self.apps.real_models:
|
|
|
|
if (model_state.app_label, model_state.name_lower) in related_models:
|
|
|
|
states_to_be_rendered.append(model_state)
|
|
|
|
|
|
|
|
# 2. All related models of migrated apps
|
|
|
|
for rel_app_label, rel_model_name in related_models:
|
|
|
|
try:
|
|
|
|
model_state = self.models[rel_app_label, rel_model_name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
states_to_be_rendered.append(model_state)
|
2015-02-09 08:11:25 +08:00
|
|
|
|
2016-11-06 20:20:08 +08:00
|
|
|
# Render all models
|
|
|
|
self.apps.render_multiple(states_to_be_rendered)
|
2013-05-19 00:30:34 +08:00
|
|
|
|
2013-05-11 00:07:13 +08:00
|
|
|
def clone(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return an exact copy of this ProjectState."""
|
2014-11-06 03:53:39 +08:00
|
|
|
new_state = ProjectState(
|
2014-12-07 05:00:09 +08:00
|
|
|
models={k: v.clone() for k, v in self.models.items()},
|
2014-05-01 03:25:12 +08:00
|
|
|
real_apps=self.real_apps,
|
2013-05-11 00:07:13 +08:00
|
|
|
)
|
2014-11-06 03:53:39 +08:00
|
|
|
if 'apps' in self.__dict__:
|
|
|
|
new_state.apps = self.apps.clone()
|
2016-11-06 19:03:05 +08:00
|
|
|
new_state.is_delayed = self.is_delayed
|
2014-11-06 03:53:39 +08:00
|
|
|
return new_state
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2016-11-06 19:03:05 +08:00
|
|
|
def clear_delayed_apps_cache(self):
|
|
|
|
if self.is_delayed and 'apps' in self.__dict__:
|
|
|
|
del self.__dict__['apps']
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
@cached_property
|
|
|
|
def apps(self):
|
|
|
|
return StateApps(self.real_apps, self.models)
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2014-12-30 09:52:32 +08:00
|
|
|
@property
|
|
|
|
def concrete_apps(self):
|
|
|
|
self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
|
|
|
|
return self.apps
|
|
|
|
|
2013-05-18 17:48:46 +08:00
|
|
|
@classmethod
|
2013-12-24 19:25:17 +08:00
|
|
|
def from_apps(cls, apps):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Take an Apps and return a ProjectState matching it."""
|
2013-10-10 23:07:48 +08:00
|
|
|
app_models = {}
|
2014-06-23 04:00:49 +08:00
|
|
|
for model in apps.get_models(include_swapped=True):
|
2014-03-26 06:30:29 +08:00
|
|
|
model_state = ModelState.from_model(model)
|
2015-01-02 23:37:21 +08:00
|
|
|
app_models[(model_state.app_label, model_state.name_lower)] = model_state
|
2013-10-10 23:07:48 +08:00
|
|
|
return cls(app_models)
|
2013-05-18 17:48:46 +08:00
|
|
|
|
2013-09-25 20:47:46 +08:00
|
|
|
def __eq__(self, other):
|
2017-08-25 16:50:01 +08:00
|
|
|
return self.models == other.models and set(self.real_apps) == set(other.real_apps)
|
2013-09-25 20:47:46 +08:00
|
|
|
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2013-12-29 03:13:08 +08:00
|
|
|
class AppConfigStub(AppConfig):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Stub of an AppConfig. Only provides a label and a dict of models."""
|
2014-01-28 04:28:53 +08:00
|
|
|
# Not used, but required by AppConfig.__init__
|
|
|
|
path = ''
|
|
|
|
|
2013-12-29 03:13:08 +08:00
|
|
|
def __init__(self, label):
|
2014-09-13 04:11:09 +08:00
|
|
|
self.label = label
|
2014-09-13 05:45:08 +08:00
|
|
|
# App-label and app-name are not the same thing, so technically passing
|
|
|
|
# in the label here is wrong. In practice, migrations don't care about
|
|
|
|
# the app name, but we need something unique, and the label works fine.
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(label, None)
|
2013-12-29 03:13:08 +08:00
|
|
|
|
2016-10-01 03:35:04 +08:00
|
|
|
def import_models(self):
|
|
|
|
self.models = self.apps.all_models[self.label]
|
2013-12-29 03:13:08 +08:00
|
|
|
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
class StateApps(Apps):
|
|
|
|
"""
|
|
|
|
Subclass of the global Apps registry class to better handle dynamic model
|
|
|
|
additions and removals.
|
|
|
|
"""
|
2014-12-30 09:52:32 +08:00
|
|
|
def __init__(self, real_apps, models, ignore_swappable=False):
|
2014-11-05 17:43:31 +08:00
|
|
|
# Any apps in self.real_apps should have all their models included
|
|
|
|
# in the render. We don't use the original model instances as there
|
|
|
|
# are some variables that refer to the Apps object.
|
|
|
|
# FKs/M2Ms from real apps are also not included as they just
|
|
|
|
# mess things up with partial states (due to lack of dependencies)
|
2015-02-09 08:11:25 +08:00
|
|
|
self.real_models = []
|
2014-11-05 17:43:31 +08:00
|
|
|
for app_label in real_apps:
|
|
|
|
app = global_apps.get_app_config(app_label)
|
|
|
|
for model in app.get_models():
|
2015-02-09 08:11:25 +08:00
|
|
|
self.real_models.append(ModelState.from_model(model, exclude_rels=True))
|
2014-11-05 17:43:31 +08:00
|
|
|
# Populate the app registry with a stub for each application.
|
|
|
|
app_labels = {model_state.app_label for model_state in models.values()}
|
|
|
|
app_configs = [AppConfigStub(label) for label in sorted(real_apps + list(app_labels))]
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(app_configs)
|
2014-11-05 17:43:31 +08:00
|
|
|
|
2016-10-01 03:08:35 +08:00
|
|
|
# The lock gets in the way of copying as implemented in clone(), which
|
|
|
|
# is called whenever Django duplicates a StateApps before updating it.
|
|
|
|
self._lock = None
|
|
|
|
|
2015-02-09 08:11:25 +08:00
|
|
|
self.render_multiple(list(models.values()) + self.real_models)
|
|
|
|
|
2015-03-03 22:12:18 +08:00
|
|
|
# There shouldn't be any operations pending at this point.
|
2016-05-18 23:18:40 +08:00
|
|
|
from django.core.checks.model_checks import _check_lazy_references
|
|
|
|
ignore = {make_model_tuple(settings.AUTH_USER_MODEL)} if ignore_swappable else set()
|
|
|
|
errors = _check_lazy_references(self, ignore=ignore)
|
|
|
|
if errors:
|
|
|
|
raise ValueError("\n".join(error.msg for error in errors))
|
2015-02-09 08:11:25 +08:00
|
|
|
|
2015-04-07 00:02:46 +08:00
|
|
|
@contextmanager
|
|
|
|
def bulk_update(self):
|
|
|
|
# Avoid clearing each model's cache for each change. Instead, clear
|
|
|
|
# all caches when we're finished updating the model instances.
|
|
|
|
ready = self.ready
|
|
|
|
self.ready = False
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.ready = ready
|
|
|
|
self.clear_cache()
|
|
|
|
|
2015-02-09 08:11:25 +08:00
|
|
|
def render_multiple(self, model_states):
|
2014-11-05 17:43:31 +08:00
|
|
|
# We keep trying to render the models in a loop, ignoring invalid
|
|
|
|
# base errors, until the size of the unrendered models doesn't
|
|
|
|
# decrease by at least one, meaning there's a base dependency loop/
|
|
|
|
# missing base.
|
2015-02-22 11:26:17 +08:00
|
|
|
if not model_states:
|
|
|
|
return
|
|
|
|
# Prevent that all model caches are expired for each render.
|
2015-04-07 00:02:46 +08:00
|
|
|
with self.bulk_update():
|
|
|
|
unrendered_models = model_states
|
|
|
|
while unrendered_models:
|
|
|
|
new_unrendered_models = []
|
|
|
|
for model in unrendered_models:
|
|
|
|
try:
|
|
|
|
model.render(self)
|
|
|
|
except InvalidBasesError:
|
|
|
|
new_unrendered_models.append(model)
|
|
|
|
if len(new_unrendered_models) == len(unrendered_models):
|
|
|
|
raise InvalidBasesError(
|
|
|
|
"Cannot resolve bases for %r\nThis can happen if you are inheriting models from an "
|
|
|
|
"app with migrations (e.g. contrib.auth)\n in an app with no migrations; see "
|
|
|
|
"https://docs.djangoproject.com/en/%s/topics/migrations/#dependencies "
|
|
|
|
"for more" % (new_unrendered_models, get_docs_version())
|
|
|
|
)
|
|
|
|
unrendered_models = new_unrendered_models
|
2014-11-05 17:43:31 +08:00
|
|
|
|
2014-11-06 03:53:39 +08:00
|
|
|
def clone(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return a clone of this registry."""
|
2014-11-06 03:53:39 +08:00
|
|
|
clone = StateApps([], {})
|
|
|
|
clone.all_models = copy.deepcopy(self.all_models)
|
|
|
|
clone.app_configs = copy.deepcopy(self.app_configs)
|
2016-10-01 03:08:35 +08:00
|
|
|
# Set the pointer to the correct app registry.
|
|
|
|
for app_config in clone.app_configs.values():
|
|
|
|
app_config.apps = clone
|
2015-02-09 08:11:25 +08:00
|
|
|
# No need to actually clone them, they'll never change
|
|
|
|
clone.real_models = self.real_models
|
2014-11-06 03:53:39 +08:00
|
|
|
return clone
|
|
|
|
|
|
|
|
def register_model(self, app_label, model):
|
|
|
|
self.all_models[app_label][model._meta.model_name] = model
|
|
|
|
if app_label not in self.app_configs:
|
|
|
|
self.app_configs[app_label] = AppConfigStub(app_label)
|
2016-10-01 03:08:35 +08:00
|
|
|
self.app_configs[app_label].apps = self
|
2014-11-06 03:53:39 +08:00
|
|
|
self.app_configs[app_label].models = OrderedDict()
|
|
|
|
self.app_configs[app_label].models[model._meta.model_name] = model
|
2015-03-03 16:43:56 +08:00
|
|
|
self.do_pending_operations(model)
|
2014-11-06 03:53:39 +08:00
|
|
|
self.clear_cache()
|
|
|
|
|
|
|
|
def unregister_model(self, app_label, model_name):
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2014-11-06 03:53:39 +08:00
|
|
|
del self.all_models[app_label][model_name]
|
|
|
|
del self.app_configs[app_label].models[model_name]
|
2017-09-07 20:16:21 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-11-06 03:53:39 +08:00
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ModelState:
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Represent a Django Model. Don't use the actual Model class as it's not
|
|
|
|
designed to have its options changed - instead, mutate this one and then
|
|
|
|
render it into a Model as required.
|
2013-05-31 01:21:32 +08:00
|
|
|
|
|
|
|
Note that while you are allowed to mutate .fields, you are not allowed
|
|
|
|
to mutate the Field instances inside there themselves - you must instead
|
|
|
|
assign new ones, as these are not detached during a clone.
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
def __init__(self, app_label, name, fields, options=None, bases=None, managers=None):
|
2013-05-11 00:07:13 +08:00
|
|
|
self.app_label = app_label
|
2017-03-04 22:47:49 +08:00
|
|
|
self.name = name
|
2013-05-30 00:47:10 +08:00
|
|
|
self.fields = fields
|
2013-05-11 00:07:13 +08:00
|
|
|
self.options = options or {}
|
2016-06-26 00:32:56 +08:00
|
|
|
self.options.setdefault('indexes', [])
|
2017-12-29 04:07:29 +08:00
|
|
|
self.bases = bases or (models.Model,)
|
2014-12-13 06:19:58 +08:00
|
|
|
self.managers = managers or []
|
2013-05-30 00:47:10 +08:00
|
|
|
# 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.")
|
2014-05-23 06:51:11 +08:00
|
|
|
for name, field in fields:
|
2015-04-07 02:17:13 +08:00
|
|
|
# Sanity-check that fields are NOT already bound to a model.
|
2014-05-23 06:51:11 +08:00
|
|
|
if hasattr(field, 'model'):
|
|
|
|
raise ValueError(
|
|
|
|
'ModelState.fields cannot be bound to a model - "%s" is.' % name
|
|
|
|
)
|
2015-04-07 02:17:13 +08:00
|
|
|
# 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
|
|
|
|
)
|
2016-06-20 23:50:05 +08:00
|
|
|
# Sanity-check that indexes have their name set.
|
|
|
|
for index in self.options['indexes']:
|
|
|
|
if not index.name:
|
|
|
|
raise ValueError(
|
|
|
|
"Indexes passed to ModelState require a name attribute. "
|
|
|
|
"%r doesn't have one." % index
|
|
|
|
)
|
2013-05-18 19:49:56 +08:00
|
|
|
|
2015-01-02 23:37:21 +08:00
|
|
|
@cached_property
|
|
|
|
def name_lower(self):
|
|
|
|
return self.name.lower()
|
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
@classmethod
|
2014-06-13 01:21:26 +08:00
|
|
|
def from_model(cls, model, exclude_rels=False):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Given a model, return a ModelState representing it."""
|
2013-05-18 19:49:56 +08:00
|
|
|
# Deconstruct the fields
|
|
|
|
fields = []
|
2013-09-01 05:18:44 +08:00
|
|
|
for field in model._meta.local_fields:
|
2015-02-26 22:19:17 +08:00
|
|
|
if getattr(field, "remote_field", None) and exclude_rels:
|
2014-06-13 01:21:26 +08:00
|
|
|
continue
|
2014-06-16 05:55:44 +08:00
|
|
|
if isinstance(field, OrderWrt):
|
|
|
|
continue
|
2017-03-04 22:47:49 +08:00
|
|
|
name = field.name
|
2013-12-05 22:19:46 +08:00
|
|
|
try:
|
2015-06-26 21:05:55 +08:00
|
|
|
fields.append((name, field.clone()))
|
2013-12-05 22:19:46 +08:00
|
|
|
except TypeError as e:
|
2015-04-27 05:05:50 +08:00
|
|
|
raise TypeError("Couldn't reconstruct field %s on %s: %s" % (
|
2013-12-05 22:19:46 +08:00
|
|
|
name,
|
2015-04-27 05:05:50 +08:00
|
|
|
model._meta.label,
|
2013-12-05 22:19:46 +08:00
|
|
|
e,
|
|
|
|
))
|
2014-06-13 01:21:26 +08:00
|
|
|
if not exclude_rels:
|
|
|
|
for field in model._meta.local_many_to_many:
|
2017-03-04 22:47:49 +08:00
|
|
|
name = field.name
|
2014-06-13 01:21:26 +08:00
|
|
|
try:
|
2015-06-26 21:05:55 +08:00
|
|
|
fields.append((name, field.clone()))
|
2014-06-13 01:21:26 +08:00
|
|
|
except TypeError as e:
|
|
|
|
raise TypeError("Couldn't reconstruct m2m field %s on %s: %s" % (
|
|
|
|
name,
|
|
|
|
model._meta.object_name,
|
|
|
|
e,
|
|
|
|
))
|
2013-05-19 18:35:17 +08:00
|
|
|
# Extract the options
|
|
|
|
options = {}
|
|
|
|
for name in DEFAULT_NAMES:
|
|
|
|
# Ignore some special options
|
2013-12-24 19:25:17 +08:00
|
|
|
if name in ["apps", "app_label"]:
|
2013-05-19 18:35:17 +08:00
|
|
|
continue
|
2013-07-02 18:19:02 +08:00
|
|
|
elif name in model._meta.original_attrs:
|
|
|
|
if name == "unique_together":
|
2013-12-06 22:05:12 +08:00
|
|
|
ut = model._meta.original_attrs["unique_together"]
|
2014-03-02 03:06:15 +08:00
|
|
|
options[name] = set(normalize_together(ut))
|
|
|
|
elif name == "index_together":
|
|
|
|
it = model._meta.original_attrs["index_together"]
|
|
|
|
options[name] = set(normalize_together(it))
|
2017-03-17 23:25:12 +08:00
|
|
|
elif name == "indexes":
|
|
|
|
indexes = [idx.clone() for idx in model._meta.indexes]
|
|
|
|
for index in indexes:
|
2017-04-07 23:47:53 +08:00
|
|
|
if not index.name:
|
|
|
|
index.set_name_with_model(model)
|
2017-03-17 23:25:12 +08:00
|
|
|
options['indexes'] = indexes
|
2013-07-02 18:19:02 +08:00
|
|
|
else:
|
|
|
|
options[name] = model._meta.original_attrs[name]
|
2014-06-13 02:12:07 +08:00
|
|
|
# If we're ignoring relationships, remove all field-listing model
|
|
|
|
# options (that option basically just means "make a stub model")
|
|
|
|
if exclude_rels:
|
|
|
|
for key in ["unique_together", "index_together", "order_with_respect_to"]:
|
|
|
|
if key in options:
|
|
|
|
del options[key]
|
2016-05-13 23:53:04 +08:00
|
|
|
# Private fields are ignored, so remove options that refer to them.
|
|
|
|
elif options.get('order_with_respect_to') in {field.name for field in model._meta.private_fields}:
|
|
|
|
del options['order_with_respect_to']
|
2014-03-05 04:17:07 +08:00
|
|
|
|
|
|
|
def flatten_bases(model):
|
|
|
|
bases = []
|
|
|
|
for base in model.__bases__:
|
|
|
|
if hasattr(base, "_meta") and base._meta.abstract:
|
|
|
|
bases.extend(flatten_bases(base))
|
|
|
|
else:
|
|
|
|
bases.append(base)
|
|
|
|
return bases
|
|
|
|
|
|
|
|
# We can't rely on __mro__ directly because we only want to flatten
|
|
|
|
# abstract models and not the whole tree. However by recursing on
|
|
|
|
# __bases__ we may end up with duplicates and ordering issues, we
|
|
|
|
# therefore discard any duplicates and reorder the bases according
|
|
|
|
# to their index in the MRO.
|
2014-03-05 07:13:15 +08:00
|
|
|
flattened_bases = sorted(set(flatten_bases(model)), key=lambda x: model.__mro__.index(x))
|
2014-03-05 04:17:07 +08:00
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
# Make our record
|
2013-09-07 01:14:09 +08:00
|
|
|
bases = tuple(
|
2014-01-20 02:09:52 +08:00
|
|
|
(
|
2015-04-27 05:05:50 +08:00
|
|
|
base._meta.label_lower
|
2014-01-20 02:09:52 +08:00
|
|
|
if hasattr(base, "_meta") else
|
|
|
|
base
|
|
|
|
)
|
2014-03-05 04:17:07 +08:00
|
|
|
for base in flattened_bases
|
2013-09-07 01:14:09 +08:00
|
|
|
)
|
2014-01-20 02:09:52 +08:00
|
|
|
# Ensure at least one base inherits from models.Model
|
2016-12-29 23:27:49 +08:00
|
|
|
if not any((isinstance(base, str) or issubclass(base, models.Model)) for base in bases):
|
2014-03-05 04:17:07 +08:00
|
|
|
bases = (models.Model,)
|
2014-12-13 06:19:58 +08:00
|
|
|
|
2016-04-17 19:55:55 +08:00
|
|
|
managers = []
|
2016-07-09 18:52:52 +08:00
|
|
|
manager_names = set()
|
2016-06-21 00:55:57 +08:00
|
|
|
default_manager_shim = None
|
|
|
|
for manager in model._meta.managers:
|
2017-03-04 22:47:49 +08:00
|
|
|
if manager.name in manager_names:
|
2016-07-09 18:52:52 +08:00
|
|
|
# Skip overridden managers.
|
|
|
|
continue
|
|
|
|
elif manager.use_in_migrations:
|
|
|
|
# Copy managers usable in migrations.
|
2016-06-21 00:55:57 +08:00
|
|
|
new_manager = copy.copy(manager)
|
|
|
|
new_manager._set_creation_counter()
|
|
|
|
elif manager is model._base_manager or manager is model._default_manager:
|
2016-07-09 18:52:52 +08:00
|
|
|
# Shim custom managers used as default and base managers.
|
2016-06-21 00:55:57 +08:00
|
|
|
new_manager = models.Manager()
|
|
|
|
new_manager.model = manager.model
|
|
|
|
new_manager.name = manager.name
|
|
|
|
if manager is model._default_manager:
|
|
|
|
default_manager_shim = new_manager
|
2015-02-06 21:24:51 +08:00
|
|
|
else:
|
2016-06-21 00:55:57 +08:00
|
|
|
continue
|
2017-03-04 22:47:49 +08:00
|
|
|
manager_names.add(manager.name)
|
|
|
|
managers.append((manager.name, new_manager))
|
2016-04-17 19:55:55 +08:00
|
|
|
|
2016-06-21 00:55:57 +08:00
|
|
|
# Ignore a shimmed default manager called objects if it's the only one.
|
|
|
|
if managers == [('objects', default_manager_shim)]:
|
|
|
|
managers = []
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
# Construct the new ModelState
|
2013-05-18 19:49:56 +08:00
|
|
|
return cls(
|
|
|
|
model._meta.app_label,
|
|
|
|
model._meta.object_name,
|
|
|
|
fields,
|
2013-05-19 18:35:17 +08:00
|
|
|
options,
|
2013-06-23 00:15:51 +08:00
|
|
|
bases,
|
2014-12-13 06:19:58 +08:00
|
|
|
managers,
|
2013-05-18 19:49:56 +08:00
|
|
|
)
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2015-01-14 01:18:23 +08:00
|
|
|
def construct_managers(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Deep-clone the managers using deconstruction."""
|
2015-01-14 01:18:23 +08:00
|
|
|
# Sort all managers by their creation counter
|
|
|
|
sorted_managers = sorted(self.managers, key=lambda v: v[1].creation_counter)
|
|
|
|
for mgr_name, manager in sorted_managers:
|
|
|
|
as_manager, manager_path, qs_path, args, kwargs = manager.deconstruct()
|
|
|
|
if as_manager:
|
|
|
|
qs_class = import_string(qs_path)
|
|
|
|
yield mgr_name, qs_class.as_manager()
|
|
|
|
else:
|
|
|
|
manager_class = import_string(manager_path)
|
|
|
|
yield mgr_name, manager_class(*args, **kwargs)
|
|
|
|
|
2014-05-23 06:51:11 +08:00
|
|
|
def clone(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return an exact copy of this ModelState."""
|
2013-05-11 00:07:13 +08:00
|
|
|
return self.__class__(
|
2013-11-03 17:22:11 +08:00
|
|
|
app_label=self.app_label,
|
|
|
|
name=self.name,
|
2015-04-07 02:17:13 +08:00
|
|
|
fields=list(self.fields),
|
2017-04-07 16:44:28 +08:00
|
|
|
# Since options are shallow-copied here, operations such as
|
|
|
|
# AddIndex must replace their option (e.g 'indexes') rather
|
|
|
|
# than mutating it.
|
2013-11-03 17:22:11 +08:00
|
|
|
options=dict(self.options),
|
|
|
|
bases=self.bases,
|
2015-04-07 02:17:13 +08:00
|
|
|
managers=list(self.managers),
|
2013-05-11 00:07:13 +08:00
|
|
|
)
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
def render(self, apps):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Create a Model object from our current state into the given apps."""
|
2013-05-11 00:07:13 +08:00
|
|
|
# First, make a Meta object
|
2017-12-11 20:08:45 +08:00
|
|
|
meta_contents = {'app_label': self.app_label, 'apps': apps, **self.options}
|
2017-06-02 07:08:59 +08:00
|
|
|
meta = type("Meta", (), meta_contents)
|
2013-05-11 00:07:13 +08:00
|
|
|
# Then, work out our bases
|
2013-12-28 21:55:54 +08:00
|
|
|
try:
|
|
|
|
bases = tuple(
|
2016-12-29 23:27:49 +08:00
|
|
|
(apps.get_model(base) if isinstance(base, str) else base)
|
2013-12-28 21:55:54 +08:00
|
|
|
for base in self.bases
|
|
|
|
)
|
|
|
|
except LookupError:
|
2013-12-06 07:55:31 +08:00
|
|
|
raise InvalidBasesError("Cannot resolve one or more bases from %r" % (self.bases,))
|
2013-05-11 00:07:13 +08:00
|
|
|
# Turn fields into a dict for the body, add other bits
|
2015-06-26 21:05:55 +08:00
|
|
|
body = {name: field.clone() for name, field in self.fields}
|
2013-05-11 00:07:13 +08:00
|
|
|
body['Meta'] = meta
|
|
|
|
body['__module__'] = "__fake__"
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
# Restore managers
|
2015-01-14 01:18:23 +08:00
|
|
|
body.update(self.construct_managers())
|
2017-01-01 02:07:35 +08:00
|
|
|
# Then, make a Model object (apps.register_model is called in __new__)
|
2017-01-19 22:48:01 +08:00
|
|
|
return type(self.name, bases, body)
|
2013-06-20 22:19:30 +08:00
|
|
|
|
|
|
|
def get_field_by_name(self, name):
|
|
|
|
for fname, field in self.fields:
|
|
|
|
if fname == name:
|
|
|
|
return field
|
|
|
|
raise ValueError("No field called %s on model %s" % (name, self.name))
|
2013-09-25 20:47:46 +08:00
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
def get_index_by_name(self, name):
|
|
|
|
for index in self.options['indexes']:
|
|
|
|
if index.name == name:
|
|
|
|
return index
|
|
|
|
raise ValueError("No index named %s on model %s" % (name, self.name))
|
|
|
|
|
2014-07-06 14:57:23 +08:00
|
|
|
def __repr__(self):
|
2016-12-01 21:08:51 +08:00
|
|
|
return "<%s: '%s.%s'>" % (self.__class__.__name__, self.app_label, self.name)
|
2014-07-06 14:57:23 +08:00
|
|
|
|
2013-09-25 20:47:46 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
|
|
|
(self.app_label == other.app_label) and
|
|
|
|
(self.name == other.name) and
|
|
|
|
(len(self.fields) == len(other.fields)) and
|
2014-10-25 12:42:44 +08:00
|
|
|
all((k1 == k2 and (f1.deconstruct()[1:] == f2.deconstruct()[1:]))
|
|
|
|
for (k1, f1), (k2, f2) in zip(self.fields, other.fields)) and
|
2013-09-25 20:47:46 +08:00
|
|
|
(self.options == other.options) and
|
2014-12-13 06:19:58 +08:00
|
|
|
(self.bases == other.bases) and
|
|
|
|
(self.managers == other.managers)
|
2013-09-25 20:47:46 +08:00
|
|
|
)
|