1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

[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
This commit is contained in:
Jason Pellerin 2006-07-19 14:57:42 +00:00
parent f350bcf058
commit 32356827b4

View File

@ -8,6 +8,7 @@ from django.db.models.query import orderlist2sql
from django.db.models import Manager from django.db.models import Manager
from bisect import bisect from bisect import bisect
import re import re
import weakref
# Calculate the verbose_name by converting from InitialCaps to "lowercase with spaces". # 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() 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: if not self.db_table:
self.db_table = "%s_%s" % (self.app_label, self.module_name) 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): def add_field(self, field):
# Insert the given field in the order in which it was created, using # Insert the given field in the order in which it was created, using
# the "creation_counter" attribute of the field. # the "creation_counter" attribute of the field.
@ -100,6 +104,12 @@ class Options(object):
return f return f
raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name) 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=''): def get_order_sql(self, table_prefix=''):
"Returns the full 'ORDER BY' clause for this object, according to self.ordering." "Returns the full 'ORDER BY' clause for this object, according to self.ordering."
if not self.ordering: return '' if not self.ordering: return ''