Ticket #14007: 14007_model_package_automatic_discovery.diff

File 14007_model_package_automatic_discovery.diff, 8.1 KB (added by Aram Dulyan, 14 years ago)

Cleanly-applying version of the patch above.

  • django/db/models/base.py

     
    1414from django.db import (connections, router, transaction, DatabaseError,
    1515    DEFAULT_DB_ALIAS)
    1616from django.db.models import signals
    17 from django.db.models.loading import register_models, get_model
     17from django.db.models.loading import register_models, get_model, MODELS_MODULE_NAME
    1818from django.utils.translation import ugettext_lazy as _
    1919import django.utils.copycompat as copy
    2020from django.utils.functional import curry, update_wrapper
     
    4545        base_meta = getattr(new_class, '_meta', None)
    4646
    4747        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           
    4952            # 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           
    5056            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]}
    5264        else:
    5365            kwargs = {}
    5466
  • django/db/models/loading.py

     
    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')
    1616
     17MODELS_MODULE_NAME = 'models'
     18
    1719class AppCache(object):
    1820    """
    1921    A cache that stores installed applications and their models. Used to
     
    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
  • tests/modeltests/model_package_automatic_discovery/models/publication.py

     
     1from django.db import models
     2
     3class 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
     2from article import Article
     3from 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
    
     
     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
  • 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
    
     
     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