Skip to content

Commit

Permalink
added first batch of review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PirosB3 committed Aug 1, 2014
1 parent 13d1d5b commit 6c27d5d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 57 deletions.
33 changes: 15 additions & 18 deletions django/apps/registry.py
Expand Up @@ -187,14 +187,14 @@ def get_models(self, app_mod=None, include_auto_created=False,
@conditional_cached_property
def related_objects_relation_graph(self):
"""
Returns two dictionaries with Options instances as keys
and a list of fields as values.
Returns two dictionaries with Options instances as keys and a list of
fields as values.
This method is used by each model to find related objects.
As this method is very expensive and is accessed frequently
(it looks up every field in a model, in every app),
it is computed on first access and then is set as a property.
The method will only cache when the apps registry is finalized.
This method is used by each model to find related objects. As this
method is very expensive and is accessed frequently (it looks up
every field in a model, in every app), it is computed on first
access and then is set as a property.
The method will only cache when the app registry is finalized.
"""
related_objects_graph = defaultdict(list)
related_objects_proxy_graph = defaultdict(list)
Expand All @@ -217,17 +217,14 @@ def related_objects_relation_graph(self):
@conditional_cached_property
def related_m2m_relation_graph(self):
"""
Returns a dictionary of Options instances
as keys and a list of fields as values.
This method is used by each model to find
related m2m. As this method is very
expensive and is accessed frequently
(it looks up every field in a model,
in every app), it is computed on first access
and then is set as a property.
The method will only cache when the apps registry
is finalized.
Returns a dictionary of Options instances as keys and a list of fields
as values.
This method is used by each model to find related m2m. As this method
is very expensive and is accessed frequently (it looks up every field
in a model, in every app), it is computed on first access and then is
set as a property.
The method will only cache when the apps registry is finalized.
"""
related_m2m_graph = defaultdict(list)

Expand Down
2 changes: 1 addition & 1 deletion django/contrib/admin/checks.py
Expand Up @@ -762,7 +762,7 @@ def _check_list_editable(self, cls, model):

def _check_list_editable_item(self, cls, model, field_name, label):
try:
field = model._meta.get_field(field_name, related_objects=True, related_m2m=True, virtual=True)
field = model._meta.get_field(field_name, related_objects=True, related_m2m=True)
except models.FieldDoesNotExist:
return refer_to_missing_field(field=field_name, option=label,
model=model, obj=cls, id='admin.E121')
Expand Down
74 changes: 37 additions & 37 deletions django/db/models/options.py
Expand Up @@ -10,7 +10,7 @@
from django.apps import apps
from django.conf import settings
from django.db.models.fields.related import ManyToManyRel, ManyToManyField
from django.db.models.fields import AutoField, FieldDoesNotExist
from django.db.models.fields import AutoField, FieldDoesNotExist, Field
from django.db.models.fields.proxy import OrderWrt
from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning
Expand All @@ -20,8 +20,6 @@
from django.utils.text import camel_case_to_spaces
from django.utils.translation import activate, deactivate_all, get_language, string_concat

from django.db.models.fields import Field


DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
'unique_together', 'permissions', 'get_latest_by',
Expand All @@ -32,24 +30,24 @@


@lru_cache(maxsize=None)
def _map_model(opts, connection):
direct = isinstance(connection, Field) or hasattr(connection, 'for_concrete_model')
model = connection.model if direct else connection.parent_model._meta.concrete_model
def _map_model(opts, link):
direct = isinstance(link, Field) or hasattr(link, 'for_concrete_model')
model = link.model if direct else link.parent_model._meta.concrete_model
if model == opts.model:
model = None
return connection, model
return link, model


@lru_cache(maxsize=None)
def _map_details(opts, connection):
direct = isinstance(connection, Field) or hasattr(connection, 'for_concrete_model')
model = connection.model if direct else connection.parent_model._meta.concrete_model
def _map_model_details(opts, link):
direct = isinstance(link, Field) or hasattr(link, 'for_concrete_model')
model = link.model if direct else link.parent_model._meta.concrete_model
if model == opts.model:
model = None

field = connection if direct else connection.field
field = connection if direct else link.field
m2m = isinstance(field, ManyToManyField)
return connection, model, direct, m2m
return link, model, direct, m2m


def normalize_together(option_together):
Expand Down Expand Up @@ -84,8 +82,7 @@ def __call__(self, fn):
def wrapper(*args, **kwargs):
warnings.warn(
"'%s is an unofficial API that has been deprecated. "
"Usage of %s may be able to be replaced with "
"a call to '%s'" % (
"You may be able to replace it with '%s'" % (
fn.__name__,
fn.__name__,
self.suggested_alternative,
Expand All @@ -99,8 +96,6 @@ def wrapper(*args, **kwargs):
@python_2_unicode_compatible
class Options(object):
def __init__(self, meta, app_label=None):
self._map_details_cache = {}
self._map_model_cache = {}
self._get_fields_cache = {}
self._get_field_cache = {}
self.local_fields = []
Expand Down Expand Up @@ -156,7 +151,7 @@ def __init__(self, meta, app_label=None):

self.default_related_name = None
self._map_model = partial(_map_model, self)
self._map_details = partial(_map_details, self)
self._map_model_details = partial(_map_model_details, self)

### INTERNAL METHODS AND PROPERTIES GO BELOW THIS LINE ###

Expand Down Expand Up @@ -402,17 +397,18 @@ def get_field(self, field_name, m2m=True, data=True, related_objects=False, rela
# we will catch the use of 'many_to_many' key and convert it to m2m.
try:
m2m = kwargs['many_to_many']
except KeyError:
pass
else:
warnings.warn(
"The 'many_to_many' argument on get_fields will be soon "
"deprecated. This parameter has changed in favor of "
"'m2m'. Please change your implementation accordingly.",
RemovedInDjango20Warning
)
except KeyError:
pass

# Creates a cache key composed of all arguments
cache_key = (m2m, data, related_objects, related_m2m, virtual,)
cache_key = (m2m, data, related_objects, related_m2m, virtual)

try:
field_map = self._get_field_cache[cache_key]
Expand Down Expand Up @@ -471,11 +467,13 @@ def get_fields(self, m2m=False, data=True, related_m2m=False, related_objects=Fa
# when displaying a ModelForm or django.contrib.admin panel and no specific ordering
# is provided. For this reason, order of field insertion must be preserved
fields = OrderedDict()
options = {'include_parents': include_parents,
'include_non_concrete': include_non_concrete,
'include_hidden': include_hidden,
'include_proxy': include_proxy,
'export_name_map': True}
options = {
'include_parents': include_parents,
'include_non_concrete': include_non_concrete,
'include_hidden': include_hidden,
'include_proxy': include_proxy,
'export_name_map': True,
}

if related_m2m:
if include_parents:
Expand All @@ -491,7 +489,7 @@ def get_fields(self, m2m=False, data=True, related_m2m=False, related_objects=Fa
fields[obj] = query_name

# Tree is computer once and cached until apps cache is expired. It is composed of
# { options_instance : [field_pointing_to_options_model, field_pointing_to_options, ..]}
# {options_instance: [field_pointing_to_options_model, field_pointing_to_options, ..]}
# If the model is a proxy model, then we also add the concrete model.
tree = self.apps.related_m2m_relation_graph
field_list = tree[self] if not self.proxy else chain(tree[self], tree[self.concrete_model._meta])
Expand Down Expand Up @@ -634,39 +632,41 @@ def local_concrete_fields(self):
# Deprecated methods
###########################################################################

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_fields_with_model(self):
return map(self._map_model, self.get_fields())

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_concrete_fields_with_model(self):
return map(self._map_model, self.get_fields(include_non_concrete=False))

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_m2m_with_model(self):
return map(self._map_model, self.get_fields(data=False, m2m=True))

@raise_deprecation(suggested_alternative="get_field")
@raise_deprecation(suggested_alternative="get_field()")
def get_field_by_name(self, name):
return self._map_details(self.get_field(name, m2m=True, related_objects=True,
related_m2m=True, virtual=True))
return self._map_model_details(self.get_field(
name, m2m=True, related_objects=True,
related_m2m=True, virtual=True
))

@raise_deprecation(suggested_alternative="field_names")
def get_all_field_names(self):
return self.field_names

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_all_related_objects(self, local_only=False, include_hidden=False,
include_proxy_eq=False):
include_parents = local_only is False
return self.get_fields(
data=False, related_objects=True,
include_parents=include_parents,
include_hidden=include_hidden,
include_proxy=include_proxy_eq
include_proxy=include_proxy_eq,
)

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_all_related_objects_with_model(self, local_only=False, include_hidden=False,
include_proxy_eq=False):
include_parents = local_only is False
Expand All @@ -678,10 +678,10 @@ def get_all_related_objects_with_model(self, local_only=False, include_hidden=Fa
)
return list(map(self._map_model, fields))

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_all_related_many_to_many_objects(self, local_only=False):
return list(self.get_fields(data=False, related_m2m=True, include_parents=local_only is not True))

@raise_deprecation(suggested_alternative="get_fields")
@raise_deprecation(suggested_alternative="get_fields()")
def get_all_related_m2m_objects_with_model(self):
return list(map(self._map_model, self.get_fields(data=False, related_m2m=True)))
2 changes: 1 addition & 1 deletion docs/internals/deprecation.txt
Expand Up @@ -41,7 +41,7 @@ about each item can often be found in the release notes of two versions prior.
* ``django.template.resolve_variable`` will be removed.

* The following methods will be removed from :class:`django.db.models.options` in
favour of the new formal Options API:
favor of the new formal Options API:

* ``get_fields_with_model()``
* ``get_concrete_fields_with_model()``
Expand Down

0 comments on commit 6c27d5d

Please sign in to comment.