Ticket #21682: 21682.diff

File 21682.diff, 3.7 KB (added by Aymeric Augustin, 10 years ago)
  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index d906f2e..0428435 100644
    a b class ModelBase(type):  
    8686            meta = attr_meta
    8787        base_meta = getattr(new_class, '_meta', None)
    8888
    89         app_label = None
    90 
    91         # Look for an application configuration to attach the model to.
    92         app_config = apps.get_containing_app_config(module)
    93 
    94         if getattr(meta, 'app_label', None) is None:
    95             if app_config is None:
    96                 if not abstract:
    97                     raise RuntimeError(
    98                         "Model class %s.%s doesn't declare an explicit "
    99                         "app_label and either isn't in an application in "
    100                         "INSTALLED_APPS or else was imported before its "
    101                         "application was loaded. " % (module, name))
    102 
    103             else:
    104                 app_label = app_config.label
     89        app_label = getattr(meta, 'app_label', None)
     90        if app_label is None:
     91            # Look for an application configuration to attach the model to.
     92            app_config = apps.get_containing_app_config(module)
     93            # Only abstract models may be created outside of an application.
     94            if app_config is None and not abstract:
     95                raise RuntimeError(
     96                    "Model class %s.%s doesn't declare an explicit "
     97                    "app_label and either isn't in an application in "
     98                    "INSTALLED_APPS or else was imported before its "
     99                    "application was loaded. " % (module, name))
     100        else:
     101            # Load for the application configuration with the given label.
     102            app_config = apps.get_app_config(app_label)
    105103
    106         new_class.add_to_class('_meta', Options(meta, app_label))
     104        new_class.add_to_class('_meta', Options(meta, app_config))
    107105        if not abstract:
    108106            new_class.add_to_class(
    109107                'DoesNotExist',
  • django/db/models/options.py

    diff --git a/django/db/models/options.py b/django/db/models/options.py
    index 83f1eb0..dc1016e 100644
    a b class Options(object):  
    8787                          'local_concrete_fields', '_forward_fields_map')
    8888    REVERSE_PROPERTIES = ('related_objects', 'fields_map', '_relation_tree')
    8989
    90     def __init__(self, meta, app_label=None):
     90    def __init__(self, meta, app_config=None):
     91        self.meta = meta
     92        self.app_config = app_config
     93
     94        self.installed = app_config is not None
     95        self.app_label = app_config.label if self.installed else None
     96
    9197        self._get_fields_cache = {}
    9298        self.proxied_children = []
    9399        self.local_fields = []
    class Options(object):  
    104110        self.default_permissions = ('add', 'change', 'delete')
    105111        self.permissions = []
    106112        self.object_name = None
    107         self.app_label = app_label
    108113        self.get_latest_by = None
    109114        self.order_with_respect_to = None
    110115        self.db_tablespace = settings.DEFAULT_TABLESPACE
    111         self.meta = meta
    112116        self.pk = None
    113117        self.has_auto_field = False
    114118        self.auto_field = None
    class Options(object):  
    170174        return link, model, direct, m2m
    171175
    172176    @property
    173     def app_config(self):
    174         # Don't go through get_app_config to avoid triggering imports.
    175         return self.apps.app_configs.get(self.app_label)
    176 
    177     @property
    178     def installed(self):
    179         return self.app_config is not None
    180 
    181     @property
    182177    def abstract_managers(self):
    183178        return [
    184179            (counter, instance.name, instance) for counter, instance, abstract
Back to Top