Moved the settings of db_table to Options.contribute_to_class().

Some fields need to know the right db_table setting in their own
contribute_to_class(), so waiting until Options._prepare() is a little
inconvenient. This is a deep-internals change. No effect on external code.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7777 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-06-29 02:35:08 +00:00
parent be6ff148c1
commit d800c0b031
2 changed files with 20 additions and 15 deletions

View File

@ -50,7 +50,15 @@ class ModelBase(type):
meta = attr_meta meta = attr_meta
base_meta = getattr(new_class, '_meta', None) base_meta = getattr(new_class, '_meta', None)
new_class.add_to_class('_meta', Options(meta)) if getattr(meta, 'app_label', None) is None:
# Figure out the app_label by looking one level up.
# For 'django.contrib.sites.models', this would be 'sites'.
model_module = sys.modules[new_class.__module__]
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
else:
kwargs = {}
new_class.add_to_class('_meta', Options(meta, **kwargs))
if not abstract: if not abstract:
new_class.add_to_class('DoesNotExist', new_class.add_to_class('DoesNotExist',
subclass_exception('DoesNotExist', ObjectDoesNotExist, module)) subclass_exception('DoesNotExist', ObjectDoesNotExist, module))
@ -71,11 +79,6 @@ class ModelBase(type):
if new_class._default_manager.model._meta.abstract: if new_class._default_manager.model._meta.abstract:
old_default_mgr = new_class._default_manager old_default_mgr = new_class._default_manager
new_class._default_manager = None new_class._default_manager = None
if getattr(new_class._meta, 'app_label', None) is None:
# Figure out the app_label by looking one level up.
# For 'django.contrib.sites.models', this would be 'sites'.
model_module = sys.modules[new_class.__module__]
new_class._meta.app_label = model_module.__name__.split('.')[-2]
# Bail out early if we have already created this class. # Bail out early if we have already created this class.
m = get_model(new_class._meta.app_label, name, False) m = get_model(new_class._meta.app_label, name, False)

View File

@ -25,7 +25,7 @@ DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
'abstract') 'abstract')
class Options(object): class Options(object):
def __init__(self, meta): def __init__(self, meta, app_label=None):
self.local_fields, self.local_many_to_many = [], [] self.local_fields, self.local_many_to_many = [], []
self.module_name, self.verbose_name = None, None self.module_name, self.verbose_name = None, None
self.verbose_name_plural = None self.verbose_name_plural = None
@ -33,7 +33,7 @@ class Options(object):
self.ordering = [] self.ordering = []
self.unique_together = [] self.unique_together = []
self.permissions = [] self.permissions = []
self.object_name, self.app_label = None, None self.object_name, self.app_label = None, app_label
self.get_latest_by = None self.get_latest_by = None
self.order_with_respect_to = None self.order_with_respect_to = None
self.db_tablespace = settings.DEFAULT_TABLESPACE self.db_tablespace = settings.DEFAULT_TABLESPACE
@ -46,6 +46,9 @@ class Options(object):
self.parents = SortedDict() self.parents = SortedDict()
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
from django.db import connection
from django.db.backends.util import truncate_name
cls._meta = self cls._meta = self
self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
# First, construct the default values for these options. # First, construct the default values for these options.
@ -87,9 +90,13 @@ class Options(object):
self.verbose_name_plural = string_concat(self.verbose_name, 's') self.verbose_name_plural = string_concat(self.verbose_name, 's')
del self.meta del self.meta
# If the db_table wasn't provided, use the app_label + module_name.
if not self.db_table:
self.db_table = "%s_%s" % (self.app_label, self.module_name)
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
def _prepare(self, model): def _prepare(self, model):
from django.db import connection
from django.db.backends.util import truncate_name
if self.order_with_respect_to: if self.order_with_respect_to:
self.order_with_respect_to = self.get_field(self.order_with_respect_to) self.order_with_respect_to = self.get_field(self.order_with_respect_to)
self.ordering = ('_order',) self.ordering = ('_order',)
@ -108,11 +115,6 @@ class Options(object):
auto_created=True) auto_created=True)
model.add_to_class('id', auto) model.add_to_class('id', auto)
# If the db_table wasn't provided, use the app_label + module_name.
if not self.db_table:
self.db_table = "%s_%s" % (self.app_label, self.module_name)
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
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.