Commit 7b63d3d3 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #12168 -- Corrected the registration of m2m autocreated models when...

Fixed #12168 -- Corrected the registration of m2m autocreated models when models.py is split into submodules. Thanks to Jens Diemer for the report and test case.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11724 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 77abadfa
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -829,9 +829,18 @@ def create_many_to_many_intermediary_model(field, klass):
        'auto_created': klass,
        'unique_together': (from_, to)
    })
    # If the models have been split into subpackages, klass.__module__
    # will be the subpackge, not the models module for the app. (See #12168)
    # Compose the actual models module name by stripping the trailing parts
    # of the namespace until we find .models
    parts = klass.__module__.split('.')
    while parts[-1] != 'models':
        parts.pop()
    module = '.'.join(parts)
    # Construct and return the new class.
    return type(name, (models.Model,), {
        'Meta': meta,
        '__module__': klass.__module__,
        '__module__': module,
        from_: models.ForeignKey(klass, related_name='%s+' % name),
        to: models.ForeignKey(to_model, related_name='%s+' % name)
    })
+1 −0
Original line number Diff line number Diff line
+3 −0
Original line number Diff line number Diff line
# Import all the models from subpackages
from article import Article
from publication import Publication
+10 −0
Original line number Diff line number Diff line
from django.db import models
from django.contrib.sites.models import Site

class Article(models.Model):
    sites = models.ManyToManyField(Site)
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField("model_package.Publication", null=True, blank=True,)

    class Meta:
        app_label = 'model_package'
+7 −0
Original line number Diff line number Diff line
from django.db import models

class Publication(models.Model):
    title = models.CharField(max_length=30)

    class Meta:
        app_label = 'model_package'
Loading