Ticket #14007: model_package_automatic_discovery.diff

File model_package_automatic_discovery.diff, 7.0 KB (added by mark@…, 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  
    1212from django.db.models.options import Options
    1313from django.db import connections, router, transaction, DatabaseError, DEFAULT_DB_ALIAS
    1414from django.db.models import signals
    15 from django.db.models.loading import register_models, get_model
     15from django.db.models.loading import register_models, get_model, MODELS_MODULE_NAME
    1616from django.utils.translation import ugettext_lazy as _
    1717import django.utils.copycompat as copy
    1818from django.utils.functional import curry, update_wrapper
    class ModelBase(type):  
    4343        base_meta = getattr(new_class, '_meta', None)
    4444
    4545        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           
    4750            # 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           
    4854            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]}
    5062        else:
    5163            kwargs = {}
    5264
  • 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  
    1212import threading
    1313
    1414__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
     17MODELS_MODULE_NAME = 'models'
    1618
    1719class AppCache(object):
    1820    """
    class AppCache(object):  
    7577        self.nesting_level += 1
    7678        app_module = import_module(app_name)
    7779        try:
    78             models = import_module('.models', app_name)
     80            models = import_module('.' + MODELS_MODULE_NAME, app_name)
    7981        except ImportError:
    8082            self.nesting_level -= 1
    8183            # If the app doesn't have a models module, we can just ignore the
    8284            # 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):
    8486                return None
    8587            # But if the app does have a models module, we need to figure out
    8688            # 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
     2from article import Article
     3from 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
    - +  
     1from django.db import models
     2
     3class 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
    - +  
     1from django.db import models
     2
     3class 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
    - +  
     1from django.db import models
     2
     3class 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
     261
     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()
     391
     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')
     60True
     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
Back to Top