Django

Code

Changeset 7777

Show
Ignore:
Timestamp:
06/28/08 21:35:08 (2 months ago)
Author:
mtredinnick
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r7722 r7777  
    5151        base_meta = getattr(new_class, '_meta', None) 
    5252 
    53         new_class.add_to_class('_meta', Options(meta)) 
     53        if getattr(meta, 'app_label', None) is None: 
     54            # Figure out the app_label by looking one level up. 
     55            # For 'django.contrib.sites.models', this would be 'sites'. 
     56            model_module = sys.modules[new_class.__module__] 
     57            kwargs = {"app_label": model_module.__name__.split('.')[-2]} 
     58        else: 
     59            kwargs = {} 
     60 
     61        new_class.add_to_class('_meta', Options(meta, **kwargs)) 
    5462        if not abstract: 
    5563            new_class.add_to_class('DoesNotExist', 
     
    7280                old_default_mgr = new_class._default_manager 
    7381            new_class._default_manager = None 
    74         if getattr(new_class._meta, 'app_label', None) is None: 
    75             # Figure out the app_label by looking one level up. 
    76             # For 'django.contrib.sites.models', this would be 'sites'. 
    77             model_module = sys.modules[new_class.__module__] 
    78             new_class._meta.app_label = model_module.__name__.split('.')[-2] 
    7982 
    8083        # Bail out early if we have already created this class. 
  • django/trunk/django/db/models/options.py

    r7764 r7777  
    2626 
    2727class Options(object): 
    28     def __init__(self, meta): 
     28    def __init__(self, meta, app_label=None): 
    2929        self.local_fields, self.local_many_to_many = [], [] 
    3030        self.module_name, self.verbose_name = None, None 
     
    3434        self.unique_together =  [] 
    3535        self.permissions =  [] 
    36         self.object_name, self.app_label = None, None 
     36        self.object_name, self.app_label = None, app_label 
    3737        self.get_latest_by = None 
    3838        self.order_with_respect_to = None 
     
    4747 
    4848    def contribute_to_class(self, cls, name): 
     49        from django.db import connection 
     50        from django.db.backends.util import truncate_name 
     51 
    4952        cls._meta = self 
    5053        self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS 
     
    8891        del self.meta 
    8992 
     93        # If the db_table wasn't provided, use the app_label + module_name. 
     94        if not self.db_table: 
     95            self.db_table = "%s_%s" % (self.app_label, self.module_name) 
     96            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) 
     97 
     98 
    9099    def _prepare(self, model): 
    91         from django.db import connection 
    92         from django.db.backends.util import truncate_name 
    93100        if self.order_with_respect_to: 
    94101            self.order_with_respect_to = self.get_field(self.order_with_respect_to) 
     
    108115                        auto_created=True) 
    109116                model.add_to_class('id', auto) 
    110  
    111         # If the db_table wasn't provided, use the app_label + module_name. 
    112         if not self.db_table: 
    113             self.db_table = "%s_%s" % (self.app_label, self.module_name) 
    114             self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) 
    115117 
    116118    def add_field(self, field):