From 32356827b42cba5adfd197e8bc2cdba440e02a05 Mon Sep 17 00:00:00 2001 From: Jason Pellerin Date: Wed, 19 Jul 2006 14:57:42 +0000 Subject: [PATCH] [multi-db] Added weakref to model in Options (self._model) and method Options.get_default_manager(). git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3379 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/options.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/django/db/models/options.py b/django/db/models/options.py index a79b48371c..0e8799db57 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -8,6 +8,7 @@ from django.db.models.query import orderlist2sql from django.db.models import Manager from bisect import bisect import re +import weakref # Calculate the verbose_name by converting from InitialCaps to "lowercase with spaces". get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip() @@ -75,6 +76,9 @@ class Options(object): if not self.db_table: self.db_table = "%s_%s" % (self.app_label, self.module_name) + # Keep a weakref to my model, for access to managers and such + self._model = weakref.ref(model) + def add_field(self, field): # Insert the given field in the order in which it was created, using # the "creation_counter" attribute of the field. @@ -100,6 +104,12 @@ class Options(object): return f raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name) + def get_default_manager(self): + model = self._model() + if model is None: + raise ReferenceError("Model no longer available in %s" % self) + return model._default_manager + def get_order_sql(self, table_prefix=''): "Returns the full 'ORDER BY' clause for this object, according to self.ordering." if not self.ordering: return ''