mirror of
https://github.com/django/django.git
synced 2025-10-07 01:00:47 +00:00
Merge branch 'master' into schema-alteration
Conflicts: django/db/backends/__init__.py django/db/backends/mysql/base.py django/db/backends/oracle/base.py django/db/backends/oracle/creation.py django/db/backends/postgresql_psycopg2/base.py django/db/backends/sqlite3/base.py django/db/models/fields/related.py
This commit is contained in:
commit
7f3678dc4c
4298 changed files with 130294 additions and 339961 deletions
|
@ -2,14 +2,15 @@ from __future__ import unicode_literals
|
|||
|
||||
import re
|
||||
from bisect import bisect
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.models.related import RelatedObject
|
||||
from django.db.models.fields.related import ManyToManyRel
|
||||
from django.db.models.fields import AutoField, FieldDoesNotExist
|
||||
from django.db.models.fields.proxy import OrderWrt
|
||||
from django.db.models.loading import get_models, app_cache_ready
|
||||
from django.utils import six
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.datastructures import SortedDict
|
||||
from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible
|
||||
from django.utils.translation import activate, deactivate_all, get_language, string_concat
|
||||
|
@ -28,7 +29,7 @@ class Options(object):
|
|||
def __init__(self, meta, app_label=None):
|
||||
self.local_fields, self.local_many_to_many = [], []
|
||||
self.virtual_fields = []
|
||||
self.module_name, self.verbose_name = None, None
|
||||
self.model_name, self.verbose_name = None, None
|
||||
self.verbose_name_plural = None
|
||||
self.db_table = ''
|
||||
self.ordering = []
|
||||
|
@ -81,7 +82,7 @@ class Options(object):
|
|||
self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
|
||||
# First, construct the default values for these options.
|
||||
self.object_name = cls.__name__
|
||||
self.module_name = self.object_name.lower()
|
||||
self.model_name = self.object_name.lower()
|
||||
self.verbose_name = get_verbose_name(self.object_name)
|
||||
|
||||
# Next, apply any overridden values from 'class Meta'.
|
||||
|
@ -119,11 +120,21 @@ class Options(object):
|
|||
self.verbose_name_plural = string_concat(self.verbose_name, 's')
|
||||
del self.meta
|
||||
|
||||
# If the db_table wasn't provided, use the app_label + module_name.
|
||||
# If the db_table wasn't provided, use the app_label + model_name.
|
||||
if not self.db_table:
|
||||
self.db_table = "%s_%s" % (self.app_label, self.module_name)
|
||||
self.db_table = "%s_%s" % (self.app_label, self.model_name)
|
||||
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
|
||||
|
||||
@property
|
||||
def module_name(self):
|
||||
"""
|
||||
This property has been deprecated in favor of `model_name`. refs #19689
|
||||
"""
|
||||
warnings.warn(
|
||||
"Options.module_name has been deprecated in favor of model_name",
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
return self.model_name
|
||||
|
||||
def _prepare(self, model):
|
||||
if self.order_with_respect_to:
|
||||
self.order_with_respect_to = self.get_field(self.order_with_respect_to)
|
||||
|
@ -165,6 +176,22 @@ class Options(object):
|
|||
if hasattr(self, '_field_cache'):
|
||||
del self._field_cache
|
||||
del self._field_name_cache
|
||||
# The fields, concrete_fields and local_concrete_fields are
|
||||
# implemented as cached properties for performance reasons.
|
||||
# The attrs will not exists if the cached property isn't
|
||||
# accessed yet, hence the try-excepts.
|
||||
try:
|
||||
del self.fields
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
del self.concrete_fields
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
del self.local_concrete_fields
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if hasattr(self, '_name_map'):
|
||||
del self._name_map
|
||||
|
@ -196,7 +223,7 @@ class Options(object):
|
|||
return '<Options for %s>' % self.object_name
|
||||
|
||||
def __str__(self):
|
||||
return "%s.%s" % (smart_text(self.app_label), smart_text(self.module_name))
|
||||
return "%s.%s" % (smart_text(self.app_label), smart_text(self.model_name))
|
||||
|
||||
def verbose_name_raw(self):
|
||||
"""
|
||||
|
@ -215,16 +242,30 @@ class Options(object):
|
|||
"""
|
||||
Has this model been swapped out for another? If so, return the model
|
||||
name of the replacement; otherwise, return None.
|
||||
|
||||
For historical reasons, model name lookups using get_model() are
|
||||
case insensitive, so we make sure we are case insensitive here.
|
||||
"""
|
||||
if self.swappable:
|
||||
model_label = '%s.%s' % (self.app_label, self.object_name)
|
||||
model_label = '%s.%s' % (self.app_label, self.model_name)
|
||||
swapped_for = getattr(settings, self.swappable, None)
|
||||
if swapped_for not in (None, model_label):
|
||||
return swapped_for
|
||||
if swapped_for:
|
||||
try:
|
||||
swapped_label, swapped_object = swapped_for.split('.')
|
||||
except ValueError:
|
||||
# setting not in the format app_label.model_name
|
||||
# raising ImproperlyConfigured here causes problems with
|
||||
# test cleanup code - instead it is raised in get_user_model
|
||||
# or as part of validation.
|
||||
return swapped_for
|
||||
|
||||
if '%s.%s' % (swapped_label, swapped_object.lower()) not in (None, model_label):
|
||||
return swapped_for
|
||||
return None
|
||||
swapped = property(_swapped)
|
||||
|
||||
def _fields(self):
|
||||
@cached_property
|
||||
def fields(self):
|
||||
"""
|
||||
The getter for self.fields. This returns the list of field objects
|
||||
available to this model (including through parent models).
|
||||
|
@ -237,7 +278,14 @@ class Options(object):
|
|||
except AttributeError:
|
||||
self._fill_fields_cache()
|
||||
return self._field_name_cache
|
||||
fields = property(_fields)
|
||||
|
||||
@cached_property
|
||||
def concrete_fields(self):
|
||||
return [f for f in self.fields if f.column is not None]
|
||||
|
||||
@cached_property
|
||||
def local_concrete_fields(self):
|
||||
return [f for f in self.local_fields if f.column is not None]
|
||||
|
||||
def get_fields_with_model(self):
|
||||
"""
|
||||
|
@ -251,6 +299,10 @@ class Options(object):
|
|||
self._fill_fields_cache()
|
||||
return self._field_cache
|
||||
|
||||
def get_concrete_fields_with_model(self):
|
||||
return [(field, model) for field, model in self.get_fields_with_model() if
|
||||
field.column is not None]
|
||||
|
||||
def _fill_fields_cache(self):
|
||||
cache = []
|
||||
for parent in self.parents:
|
||||
|
@ -356,18 +408,21 @@ class Options(object):
|
|||
cache[f.name] = (f, model, True, True)
|
||||
for f, model in self.get_fields_with_model():
|
||||
cache[f.name] = (f, model, True, False)
|
||||
for f in self.virtual_fields:
|
||||
if hasattr(f, 'related'):
|
||||
cache[f.name] = (f.related, None if f.model == self.model else f.model, True, False)
|
||||
if app_cache_ready():
|
||||
self._name_map = cache
|
||||
return cache
|
||||
|
||||
def get_add_permission(self):
|
||||
return 'add_%s' % self.object_name.lower()
|
||||
return 'add_%s' % self.model_name
|
||||
|
||||
def get_change_permission(self):
|
||||
return 'change_%s' % self.object_name.lower()
|
||||
return 'change_%s' % self.model_name
|
||||
|
||||
def get_delete_permission(self):
|
||||
return 'delete_%s' % self.object_name.lower()
|
||||
return 'delete_%s' % self.model_name
|
||||
|
||||
def get_all_related_objects(self, local_only=False, include_hidden=False,
|
||||
include_proxy_eq=False):
|
||||
|
@ -411,12 +466,12 @@ class Options(object):
|
|||
for klass in get_models(include_auto_created=True, only_installed=False):
|
||||
if not klass._meta.swapped:
|
||||
for f in klass._meta.local_fields:
|
||||
if f.rel and not isinstance(f.rel.to, six.string_types):
|
||||
if f.rel and not isinstance(f.rel.to, six.string_types) and f.generate_reverse_relation:
|
||||
if self == f.rel.to._meta:
|
||||
cache[RelatedObject(f.rel.to, klass, f)] = None
|
||||
proxy_cache[RelatedObject(f.rel.to, klass, f)] = None
|
||||
cache[f.related] = None
|
||||
proxy_cache[f.related] = None
|
||||
elif self.concrete_model == f.rel.to._meta.concrete_model:
|
||||
proxy_cache[RelatedObject(f.rel.to, klass, f)] = None
|
||||
proxy_cache[f.related] = None
|
||||
self._related_objects_cache = cache
|
||||
self._related_objects_proxy_cache = proxy_cache
|
||||
|
||||
|
@ -457,7 +512,7 @@ class Options(object):
|
|||
if (f.rel
|
||||
and not isinstance(f.rel.to, six.string_types)
|
||||
and self == f.rel.to._meta):
|
||||
cache[RelatedObject(f.rel.to, klass, f)] = None
|
||||
cache[f.related] = None
|
||||
if app_cache_ready():
|
||||
self._related_many_to_many_cache = cache
|
||||
return cache
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue