Ticket #14007: model_package_automatic_discovery.diff
File model_package_automatic_discovery.diff, 7.0 KB (added by , 14 years ago) |
---|
-
django/db/models/base.py
diff --git a/django/db/models/base.py b/django/db/models/base.py index 6304e00..e96e098 100644
a b from django.db.models.query_utils import CollectedObjects, DeferredAttribute 12 12 from django.db.models.options import Options 13 13 from django.db import connections, router, transaction, DatabaseError, DEFAULT_DB_ALIAS 14 14 from django.db.models import signals 15 from django.db.models.loading import register_models, get_model 15 from django.db.models.loading import register_models, get_model, MODELS_MODULE_NAME 16 16 from django.utils.translation import ugettext_lazy as _ 17 17 import django.utils.copycompat as copy 18 18 from django.utils.functional import curry, update_wrapper … … class ModelBase(type): 43 43 base_meta = getattr(new_class, '_meta', None) 44 44 45 45 if getattr(meta, 'app_label', None) is None: 46 # Figure out the app_label by looking one level up. 46 # Figure out the app_label by looking one level up from the package or module named 'models'. 47 # If no such package or module exists, fall back to the behavior of looking one level up from 48 # the module this model is defined in. 49 47 50 # For 'django.contrib.sites.models', this would be 'sites'. 51 # For 'geo.models.places' this would be 'geo'. 52 # For 'polymorphic.polymorphic_model' this would be 'polymorphic' 53 48 54 model_module = sys.modules[new_class.__module__] 49 kwargs = {"app_label": model_module.__name__.split('.')[-2]} 55 package_components = model_module.__name__.split('.') 56 package_components.reverse() # For finding the last occurance of 'models' 57 try: 58 app_label_index = package_components.index(MODELS_MODULE_NAME) + 1 59 except ValueError: 60 app_label_index = 1 61 kwargs = {"app_label": package_components[app_label_index]} 50 62 else: 51 63 kwargs = {} 52 64 -
django/db/models/loading.py
diff --git a/django/db/models/loading.py b/django/db/models/loading.py index 620cebc..db76202 100644
a b import os 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 17 MODELS_MODULE_NAME = 'models' 16 18 17 19 class AppCache(object): 18 20 """ … … class AppCache(object): 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 -
new file tests/modeltests/model_package_automatic_discovery/__init__.py
diff --git a/tests/modeltests/model_package_automatic_discovery/__init__.py b/tests/modeltests/model_package_automatic_discovery/__init__.py new file mode 100644 index 0000000..8b13789
- + 1 -
new file tests/modeltests/model_package_automatic_discovery/models/__init__.py
diff --git a/tests/modeltests/model_package_automatic_discovery/models/__init__.py b/tests/modeltests/model_package_automatic_discovery/models/__init__.py new file mode 100644 index 0000000..91e1b02
- + 1 # Import all the models from subpackages 2 from article import Article 3 from publication import Publication -
new file tests/modeltests/model_package_automatic_discovery/models/article.py
diff --git a/tests/modeltests/model_package_automatic_discovery/models/article.py b/tests/modeltests/model_package_automatic_discovery/models/article.py new file mode 100644 index 0000000..6b4ea27
- + 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 -
new file tests/modeltests/model_package_automatic_discovery/models/publication.py
diff --git a/tests/modeltests/model_package_automatic_discovery/models/publication.py b/tests/modeltests/model_package_automatic_discovery/models/publication.py new file mode 100644 index 0000000..7109918
- + 1 from django.db import models 2 3 class Publication(models.Model): 4 title = models.CharField(max_length=30) -
new file tests/modeltests/model_package_automatic_discovery/tests.py
diff --git a/tests/modeltests/model_package_automatic_discovery/tests.py b/tests/modeltests/model_package_automatic_discovery/tests.py new file mode 100644 index 0000000..266ff4c
- + 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