From 2b2db12032e47229a9d9af28b05a07648f3fef93 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 14 Jan 2010 14:55:36 +0000 Subject: [PATCH] Fixed #12386 -- Corrected the column names produced for autogenerated m2m tables when the related table is specified as a dot-separated string. Thanks to ldevesine for the report and simonb for the extra test case. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12226 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 2 +- tests/modeltests/model_package/tests.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index f3962f17b2..e3d4a18264 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -881,8 +881,8 @@ def create_many_to_many_intermediary_model(field, klass): from django.db import models managed = True if isinstance(field.rel.to, basestring) and field.rel.to != RECURSIVE_RELATIONSHIP_CONSTANT: - to = field.rel.to to_model = field.rel.to + to = to_model.split('.')[-1] def set_managed(field, model, cls): field.rel.through._meta.managed = model._meta.managed or cls._meta.managed add_lazy_relation(klass, field, to_model, set_managed) diff --git a/tests/modeltests/model_package/tests.py b/tests/modeltests/model_package/tests.py index 7fd4b6f679..4d154c14fa 100644 --- a/tests/modeltests/model_package/tests.py +++ b/tests/modeltests/model_package/tests.py @@ -50,6 +50,31 @@ __test__ = {'API_TESTS': """ >>> ad.publications.count() 1 +# Regression for #12386 - field names on the autogenerated intermediate class +# that are specified as dotted strings don't retain any path component for the +# field or column name + +>>> Article.publications.through._meta.fields[1].name +'article' + +>>> Article.publications.through._meta.fields[1].get_attname_column() +('article_id', 'article_id') + +>>> Article.publications.through._meta.fields[2].name +'publication' + +>>> Article.publications.through._meta.fields[2].get_attname_column() +('publication_id', 'publication_id') + +>>> Article._meta.get_field('publications').m2m_db_table() +'model_package_article_publications' + +>>> Article._meta.get_field('publications').m2m_column_name() +'article_id' + +>>> Article._meta.get_field('publications').m2m_reverse_name() +'publication_id' + """}