Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit d800c0b031aea81ee6f3a7c28ef88c05494e4eea 1 parent be6ff14
@malcolmt malcolmt authored
Showing with 20 additions and 15 deletions.
  1. +9 −6 django/db/models/base.py
  2. +11 −9 django/db/models/options.py
View
15 django/db/models/base.py
@@ -50,7 +50,15 @@ def __new__(cls, name, bases, attrs):
meta = attr_meta
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:
new_class.add_to_class('DoesNotExist',
subclass_exception('DoesNotExist', ObjectDoesNotExist, module))
@@ -71,11 +79,6 @@ def __new__(cls, name, bases, attrs):
if new_class._default_manager.model._meta.abstract:
old_default_mgr = new_class._default_manager
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.
m = get_model(new_class._meta.app_label, name, False)
View
20 django/db/models/options.py
@@ -25,7 +25,7 @@
'abstract')
class Options(object):
- def __init__(self, meta):
+ def __init__(self, meta, app_label=None):
self.local_fields, self.local_many_to_many = [], []
self.module_name, self.verbose_name = None, None
self.verbose_name_plural = None
@@ -33,7 +33,7 @@ def __init__(self, meta):
self.ordering = []
self.unique_together = []
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.order_with_respect_to = None
self.db_tablespace = settings.DEFAULT_TABLESPACE
@@ -46,6 +46,9 @@ def __init__(self, meta):
self.parents = SortedDict()
def contribute_to_class(self, cls, name):
+ from django.db import connection
+ from django.db.backends.util import truncate_name
+
cls._meta = self
self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
# First, construct the default values for these options.
@@ -87,9 +90,13 @@ def contribute_to_class(self, cls, name):
self.verbose_name_plural = string_concat(self.verbose_name, 's')
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):
- from django.db import connection
- from django.db.backends.util import truncate_name
if self.order_with_respect_to:
self.order_with_respect_to = self.get_field(self.order_with_respect_to)
self.ordering = ('_order',)
@@ -108,11 +115,6 @@ def _prepare(self, model):
auto_created=True)
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):
# Insert the given field in the order in which it was created, using
# the "creation_counter" attribute of the field.
Please sign in to comment.
Something went wrong with that request. Please try again.