From 3647c0a49a2f4535b8a9aba40e662743e4d53e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Fri, 18 Jan 2013 07:09:39 +0200 Subject: [PATCH] Avoided unnecessary recreation of RelatedObjects Refs #19399. Thanks to Track alias KJ for the patch. --- django/core/management/validation.py | 5 ++--- django/db/models/options.py | 9 ++++----- django/forms/models.py | 3 --- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/django/core/management/validation.py b/django/core/management/validation.py index c0452c5aa6..f49a3c2232 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -27,7 +27,6 @@ def get_validation_errors(outfile, app=None): """ from django.db import models, connection from django.db.models.loading import get_app_errors - from django.db.models.fields.related import RelatedObject from django.db.models.deletion import SET_NULL, SET_DEFAULT e = ModelErrorCollection(outfile) @@ -154,7 +153,7 @@ def get_validation_errors(outfile, app=None): e.add(opts, "Field '%s' under model '%s' must have a unique=True constraint." % (f.rel.field_name, f.rel.to.__name__)) rel_opts = f.rel.to._meta - rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() + rel_name = f.related.get_accessor_name() rel_query_name = f.related_query_name() if not f.rel.is_hidden(): for r in rel_opts.fields: @@ -278,7 +277,7 @@ def get_validation_errors(outfile, app=None): ) rel_opts = f.rel.to._meta - rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name() + rel_name = f.related.get_accessor_name() rel_query_name = f.related_query_name() # If rel_name is none, there is no reverse accessor (this only # occurs for symmetrical m2m relations to self). If this is the diff --git a/django/db/models/options.py b/django/db/models/options.py index 45b32b0ea4..952596b514 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -4,7 +4,6 @@ import re from bisect import bisect 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 @@ -424,10 +423,10 @@ class Options(object): for f in klass._meta.local_fields: if f.rel and not isinstance(f.rel.to, six.string_types): 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 @@ -468,7 +467,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 diff --git a/django/forms/models.py b/django/forms/models.py index 1b6821cd5b..74886d7ae0 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -702,14 +702,11 @@ class BaseInlineFormSet(BaseModelFormSet): """A formset for child objects related to a parent.""" def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None, **kwargs): - from django.db.models.fields.related import RelatedObject if instance is None: self.instance = self.fk.rel.to() else: self.instance = instance self.save_as_new = save_as_new - # is there a better way to get the object descriptor? - self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name() if queryset is None: queryset = self.model._default_manager if self.instance.pk: