Fixed #12245 -- Corrected target app handling for auto-generated m2m models when the parent model isn't in the models module (or a subpackage thereof). Thanks to emulbreh for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11755 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2009-11-20 00:59:38 +00:00
parent e6b4d1014e
commit 660f9086f1
2 changed files with 26 additions and 12 deletions

View File

@ -835,20 +835,13 @@ def create_many_to_many_intermediary_model(field, klass):
'db_table': field._get_m2m_db_table(klass._meta), 'db_table': field._get_m2m_db_table(klass._meta),
'managed': managed, 'managed': managed,
'auto_created': klass, 'auto_created': klass,
'app_label': klass._meta.app_label,
'unique_together': (from_, to) '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. # Construct and return the new class.
return type(name, (models.Model,), { return type(name, (models.Model,), {
'Meta': meta, 'Meta': meta,
'__module__': module, '__module__': klass.__module__,
from_: models.ForeignKey(klass, related_name='%s+' % name), from_: models.ForeignKey(klass, related_name='%s+' % name),
to: models.ForeignKey(to_model, related_name='%s+' % name) to: models.ForeignKey(to_model, related_name='%s+' % name)
}) })

View File

@ -1,4 +1,13 @@
""" from django.db import models
class Advertisment(models.Model):
customer = models.CharField(max_length=100)
publications = models.ManyToManyField("model_package.Publication", null=True, blank=True)
class Meta:
app_label = 'model_package'
__test__ = {'API_TESTS': """
>>> from models.publication import Publication >>> from models.publication import Publication
>>> from models.article import Article >>> from models.article import Article
>>> from django.contrib.auth.views import Site >>> from django.contrib.auth.views import Site
@ -19,7 +28,6 @@
>>> a.save() >>> a.save()
>>> a.publications.add(p) >>> a.publications.add(p)
>>> a.sites.add(current_site) >>> a.sites.add(current_site)
>>> a.save()
>>> a = Article.objects.get(id=1) >>> a = Article.objects.get(id=1)
>>> a >>> a
@ -29,6 +37,19 @@
>>> a.sites.count() >>> a.sites.count()
1 1
""" # Regression for #12248 - Models can exist in the test package, too
>>> ad = Advertisment(customer="Lawrence Journal-World")
>>> ad.save()
>>> ad.publications.add(p)
>>> ad = Advertisment.objects.get(id=1)
>>> ad
<Advertisment: Advertisment object>
>>> ad.publications.count()
1
"""}