Ticket #14007: 14007_model_package_automatic_discovery.diff
File 14007_model_package_automatic_discovery.diff, 8.1 KB (added by , 14 years ago) |
---|
-
django/db/models/base.py
14 14 from django.db import (connections, router, transaction, DatabaseError, 15 15 DEFAULT_DB_ALIAS) 16 16 from django.db.models import signals 17 from django.db.models.loading import register_models, get_model 17 from django.db.models.loading import register_models, get_model, MODELS_MODULE_NAME 18 18 from django.utils.translation import ugettext_lazy as _ 19 19 import django.utils.copycompat as copy 20 20 from django.utils.functional import curry, update_wrapper … … 45 45 base_meta = getattr(new_class, '_meta', None) 46 46 47 47 if getattr(meta, 'app_label', None) is None: 48 # Figure out the app_label by looking one level up. 48 # Figure out the app_label by looking one level up from the package or module named 'models'. 49 # If no such package or module exists, fall back to the behavior of looking one level up from 50 # the module this model is defined in. 51 49 52 # For 'django.contrib.sites.models', this would be 'sites'. 53 # For 'geo.models.places' this would be 'geo'. 54 # For 'polymorphic.polymorphic_model' this would be 'polymorphic' 55 50 56 model_module = sys.modules[new_class.__module__] 51 kwargs = {"app_label": model_module.__name__.split('.')[-2]} 57 package_components = model_module.__name__.split('.') 58 package_components.reverse() # For finding the last occurance of 'models' 59 try: 60 app_label_index = package_components.index(MODELS_MODULE_NAME) + 1 61 except ValueError: 62 app_label_index = 1 63 kwargs = {"app_label": package_components[app_label_index]} 52 64 else: 53 65 kwargs = {} 54 66 -
django/db/models/loading.py
12 12 import threading 13 13 14 14 __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models', 15 'load_app', 'app_cache_ready' )15 'load_app', 'app_cache_ready', 'MODELS_MODULE_NAME') 16 16 17 MODELS_MODULE_NAME = 'models' 18 17 19 class AppCache(object): 18 20 """ 19 21 A cache that stores installed applications and their models. Used to … … 75 77 self.nesting_level += 1 76 78 app_module = import_module(app_name) 77 79 try: 78 models = import_module('. models', app_name)80 models = import_module('.' + MODELS_MODULE_NAME, app_name) 79 81 except ImportError: 80 82 self.nesting_level -= 1 81 83 # If the app doesn't have a models module, we can just ignore the 82 84 # ImportError and return no models for it. 83 if not module_has_submodule(app_module, 'models'):85 if not module_has_submodule(app_module, MODELS_MODULE_NAME): 84 86 return None 85 87 # But if the app does have a models module, we need to figure out 86 88 # whether to suppress or propagate the error. If can_postpone is -
tests/modeltests/model_package_automatic_discovery/models/publication.py
1 from django.db import models 2 3 class Publication(models.Model): 4 title = models.CharField(max_length=30) -
tests/modeltests/model_package_automatic_discovery/models/__init__.py
Property changes on: tests/modeltests/model_package_automatic_discovery/models/publication.py ___________________________________________________________________ Added: svn:mime-type + text/plain
1 # Import all the models from subpackages 2 from article import Article 3 from publication import Publication -
tests/modeltests/model_package_automatic_discovery/models/article.py
Property changes on: tests/modeltests/model_package_automatic_discovery/models/__init__.py ___________________________________________________________________ Added: svn:mime-type + text/plain
1 from django.db import models 2 3 class Article(models.Model): 4 headline = models.CharField(max_length=100) 5 publications = models.ManyToManyField("model_package_automatic_discovery.Publication", null=True, blank=True,) 6 No newline at end of file -
tests/modeltests/model_package_automatic_discovery/__init__.py
Property changes on: tests/modeltests/model_package_automatic_discovery/models/article.py ___________________________________________________________________ Added: svn:mime-type + text/plain
1 -
tests/modeltests/model_package_automatic_discovery/tests.py
Property changes on: tests/modeltests/model_package_automatic_discovery/__init__.py ___________________________________________________________________ Added: svn:mime-type + text/plain
1 from django.db import models 2 3 class Advertisment(models.Model): 4 customer = models.CharField(max_length=100) 5 publications = models.ManyToManyField("model_package_automatic_discovery.Publication", null=True, blank=True) 6 7 __test__ = {'API_TESTS': """ 8 >>> from models.publication import Publication 9 >>> from models.article import Article 10 11 >>> p = Publication(title="FooBar") 12 >>> p.save() 13 >>> p 14 <Publication: Publication object> 15 16 # Regression for #12168: models split into subpackages still get M2M tables 17 18 >>> a = Article(headline="a foo headline") 19 >>> a.save() 20 >>> a.publications.add(p) 21 22 >>> a = Article.objects.get(id=1) 23 >>> a 24 <Article: Article object> 25 >>> a.id 26 1 27 28 # Regression for #12245 - Models can exist in the test package, too 29 30 >>> ad = Advertisment(customer="Lawrence Journal-World") 31 >>> ad.save() 32 >>> ad.publications.add(p) 33 34 >>> ad = Advertisment.objects.get(id=1) 35 >>> ad 36 <Advertisment: Advertisment object> 37 38 >>> ad.publications.count() 39 1 40 41 # Regression for #12386 - field names on the autogenerated intermediate class 42 # that are specified as dotted strings don't retain any path component for the 43 # field or column name 44 45 >>> Article.publications.through._meta.fields[1].name 46 'article' 47 48 >>> Article.publications.through._meta.fields[1].get_attname_column() 49 ('article_id', 'article_id') 50 51 >>> Article.publications.through._meta.fields[2].name 52 'publication' 53 54 >>> Article.publications.through._meta.fields[2].get_attname_column() 55 ('publication_id', 'publication_id') 56 57 # The oracle backend truncates the name to 'model_package_automatic_di4b50'. 58 >>> Article._meta.get_field('publications').m2m_db_table() \\ 59 ... in ('model_package_automatic_discovery_article_publications', 'model_package_automatic_di4b50') 60 True 61 62 >>> Article._meta.get_field('publications').m2m_column_name() 63 'article_id' 64 65 >>> Article._meta.get_field('publications').m2m_reverse_name() 66 'publication_id' 67 68 """} 69 70