From 2f9eef1227aa5d10c57995a5363696e8adc276b6 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 27 Apr 2010 12:35:49 +0000 Subject: [PATCH] Fixed #13407 -- Corrected verbose names for autogenerated m2m models, and cleaned up the default form prefix when an autogenerated m2m through model is used in a formset. Thanks to carljm for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13029 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 4 +++- django/forms/models.py | 2 +- tests/regressiontests/admin_inlines/models.py | 14 ++++++++++++++ tests/regressiontests/admin_inlines/tests.py | 10 ++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 1e016fa7e1..c6fcdac845 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -969,7 +969,9 @@ def create_many_to_many_intermediary_model(field, klass): 'managed': managed, 'auto_created': klass, 'app_label': klass._meta.app_label, - 'unique_together': (from_, to) + 'unique_together': (from_, to), + 'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to}, + 'verbose_name_plural': _('%(from)s-%(to)s relationships') % {'from': from_, 'to': to}, }) # Construct and return the new class. return type(name, (models.Model,), { diff --git a/django/forms/models.py b/django/forms/models.py index 42de898aa7..4ac3f950ba 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -718,7 +718,7 @@ class BaseInlineFormSet(BaseModelFormSet): #@classmethod def get_default_prefix(cls): from django.db.models.fields.related import RelatedObject - return RelatedObject(cls.fk.rel.to, cls.model, cls.fk).get_accessor_name() + return RelatedObject(cls.fk.rel.to, cls.model, cls.fk).get_accessor_name().replace('+','') get_default_prefix = classmethod(get_default_prefix) def save_new(self, form, commit=True): diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py index 2c6a64dc7a..bca582b9ab 100644 --- a/tests/regressiontests/admin_inlines/models.py +++ b/tests/regressiontests/admin_inlines/models.py @@ -30,6 +30,20 @@ class Child(models.Model): def __unicode__(self): return u'I am %s, a child of %s' % (self.name, self.parent) +class Book(models.Model): + name = models.CharField(max_length=50) + +class Author(models.Model): + name = models.CharField(max_length=50) + books = models.ManyToManyField(Book) + +class BookInline(admin.TabularInline): + model = Author.books.through + +class AuthorAdmin(admin.ModelAdmin): + inlines = [BookInline] + +admin.site.register(Author, AuthorAdmin) class Holder(models.Model): dummy = models.IntegerField() diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py index 1ac0100b4a..ffcbe1e4ba 100644 --- a/tests/regressiontests/admin_inlines/tests.py +++ b/tests/regressiontests/admin_inlines/tests.py @@ -38,6 +38,16 @@ class TestInline(TestCase): % holder.id) self.assertContains(response, '') + def test_many_to_many_inlines(self): + "Autogenerated many-to-many inlines are displayed correctly (#13407)" + response = self.client.get('/test_admin/admin/admin_inlines/author/add/') + # The heading for the m2m inline block uses the right text + self.assertContains(response, '

Author-book relationships

') + # The "add another" label is correct + self.assertContains(response, 'Add another Author-Book Relationship') + # The '+' is dropped from the autogenerated form prefix (Author_books+) + self.assertContains(response, 'id="id_Author_books-TOTAL_FORMS"') + class TestInlineMedia(TestCase): fixtures = ['admin-views-users.xml']