From 3ee086f80d785d6242292e0b4f3a289cbaec75a1 Mon Sep 17 00:00:00 2001 From: Robert Wittams Date: Tue, 20 Dec 2005 23:52:18 +0000 Subject: [PATCH] magic-removal: Moved related field processing to module level functions. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1751 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 43 +++++++++++++++--------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index ccdb59e799..7543fc6de5 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -11,28 +11,29 @@ TABULAR, STACKED = 1, 2 RECURSIVE_RELATIONSHIP_CONSTANT = 'self' +pending_lookups = {} + +def add_lookup(rel_cls, field): + name = field.rel.to + module = rel_cls.__module__ + key = (module, name) + pending_lookups.setdefault(key, []).append((rel_cls, field)) + + +def do_pending_lookups(sender): + other_cls = sender + key = (other_cls.__module__, other_cls.__name__) + for rel_cls, field in pending_lookups.setdefault(key,[]): + field.rel.to = other_cls + field.do_related_class(other_cls, rel_cls) + +dispatcher.connect( + do_pending_lookups, + signal = signals.class_prepared +) + #HACK class RelatedField(object): - pending_lookups = {} - - dispatcher.connect( - lambda sender: RelatedField.do_pending_lookups(sender), - signal = signals.class_prepared, - weak = False) - - def add_lookup(cls, rel_cls, field): - name = field.rel.to - module = rel_cls.__module__ - key = (module, name) - cls.pending_lookups.setdefault(key, []).append((rel_cls, field)) - add_lookup = classmethod(add_lookup) - - def do_pending_lookups(cls, other_cls): - key = (other_cls.__module__, other_cls.__name__) - for rel_cls, field in cls.pending_lookups.setdefault(key,[]): - field.rel.to = other_cls - field.do_related_class(other_cls, rel_cls) - do_pending_lookups = classmethod(do_pending_lookups) def contribute_to_class(self, cls, name): sup = super(RelatedField, self) @@ -42,7 +43,7 @@ class RelatedField(object): if isinstance(other, basestring): if other == RECURSIVE_RELATIONSHIP_CONSTANT: self.rel.to = cls.__name__ - self.add_lookup(cls, self) + add_lookup(cls, self) else: self.do_related_class(other, cls)