Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6214 -- Added some small optimisations to model class initiali…

…sation.

Thanks, Ivan Illarionov.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7098 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5480a1eecfe384799740666d52090e9960ee833c 1 parent 2bb9e0a
Malcolm Tredinnick authored February 08, 2008

Showing 1 changed file with 33 additions and 36 deletions. Show diff stats Hide diff stats

  1. 69  django/db/models/base.py
69  django/db/models/base.py
@@ -24,7 +24,8 @@ class ModelBase(type):
24 24
     def __new__(cls, name, bases, attrs):
25 25
         # If this isn't a subclass of Model, don't do anything special.
26 26
         try:
27  
-            if not filter(lambda b: issubclass(b, Model), bases):
  27
+            parents = [b for b in bases if issubclass(b, Model)]
  28
+            if not parents:
28 29
                 return super(ModelBase, cls).__new__(cls, name, bases, attrs)
29 30
         except NameError:
30 31
             # 'Model' isn't defined yet, meaning we're looking at Django's own
@@ -39,9 +40,8 @@ def __new__(cls, name, bases, attrs):
39 40
             types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned, ), {}))
40 41
 
41 42
         # Build complete list of parents
42  
-        for base in bases:
43  
-            # TODO: Checking for the presence of '_meta' is hackish.
44  
-            if '_meta' in dir(base):
  43
+        for base in parents:
  44
+            if base is not Model:
45 45
                 new_class._meta.parents.append(base)
46 46
                 new_class._meta.parents.extend(base._meta.parents)
47 47
 
@@ -79,6 +79,35 @@ def __new__(cls, name, bases, attrs):
79 79
         # registered version.
80 80
         return get_model(new_class._meta.app_label, name, False)
81 81
 
  82
+    def add_to_class(cls, name, value):
  83
+        if name == 'Admin':
  84
+            assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value))
  85
+            value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')]))
  86
+        if hasattr(value, 'contribute_to_class'):
  87
+            value.contribute_to_class(cls, name)
  88
+        else:
  89
+            setattr(cls, name, value)
  90
+
  91
+    def _prepare(cls):
  92
+        # Creates some methods once self._meta has been populated.
  93
+        opts = cls._meta
  94
+        opts._prepare(cls)
  95
+
  96
+        if opts.order_with_respect_to:
  97
+            cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
  98
+            cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
  99
+            setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
  100
+            setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
  101
+
  102
+        # Give the class a docstring -- its definition.
  103
+        if cls.__doc__ is None:
  104
+            cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
  105
+
  106
+        if hasattr(cls, 'get_absolute_url'):
  107
+            cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
  108
+
  109
+        dispatcher.send(signal=signals.class_prepared, sender=cls)
  110
+
82 111
 class Model(object):
83 112
     __metaclass__ = ModelBase
84 113
 
@@ -176,38 +205,6 @@ def __init__(self, *args, **kwargs):
176 205
                 raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
177 206
         dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
178 207
 
179  
-    def add_to_class(cls, name, value):
180  
-        if name == 'Admin':
181  
-            assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value))
182  
-            value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')]))
183  
-        if hasattr(value, 'contribute_to_class'):
184  
-            value.contribute_to_class(cls, name)
185  
-        else:
186  
-            setattr(cls, name, value)
187  
-    add_to_class = classmethod(add_to_class)
188  
-
189  
-    def _prepare(cls):
190  
-        # Creates some methods once self._meta has been populated.
191  
-        opts = cls._meta
192  
-        opts._prepare(cls)
193  
-
194  
-        if opts.order_with_respect_to:
195  
-            cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
196  
-            cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
197  
-            setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
198  
-            setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
199  
-
200  
-        # Give the class a docstring -- its definition.
201  
-        if cls.__doc__ is None:
202  
-            cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
203  
-
204  
-        if hasattr(cls, 'get_absolute_url'):
205  
-            cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
206  
-
207  
-        dispatcher.send(signal=signals.class_prepared, sender=cls)
208  
-
209  
-    _prepare = classmethod(_prepare)
210  
-
211 208
     def save(self, raw=False):
212 209
         dispatcher.send(signal=signals.pre_save, sender=self.__class__,
213 210
                         instance=self, raw=raw)

0 notes on commit 5480a1e

Please sign in to comment.
Something went wrong with that request. Please try again.