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
This commit is contained in:
Russell Keith-Magee 2010-04-27 12:35:49 +00:00
parent ffe79b0979
commit 2f9eef1227
4 changed files with 28 additions and 2 deletions

View File

@ -969,7 +969,9 @@ def create_many_to_many_intermediary_model(field, klass):
'managed': managed, 'managed': managed,
'auto_created': klass, 'auto_created': klass,
'app_label': klass._meta.app_label, '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. # Construct and return the new class.
return type(name, (models.Model,), { return type(name, (models.Model,), {

View File

@ -718,7 +718,7 @@ class BaseInlineFormSet(BaseModelFormSet):
#@classmethod #@classmethod
def get_default_prefix(cls): def get_default_prefix(cls):
from django.db.models.fields.related import RelatedObject 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) get_default_prefix = classmethod(get_default_prefix)
def save_new(self, form, commit=True): def save_new(self, form, commit=True):

View File

@ -30,6 +30,20 @@ class Child(models.Model):
def __unicode__(self): def __unicode__(self):
return u'I am %s, a child of %s' % (self.name, self.parent) 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): class Holder(models.Model):
dummy = models.IntegerField() dummy = models.IntegerField()

View File

@ -38,6 +38,16 @@ class TestInline(TestCase):
% holder.id) % holder.id)
self.assertContains(response, '<label>Inner readonly label:</label>') self.assertContains(response, '<label>Inner readonly label:</label>')
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, '<h2>Author-book relationships</h2>')
# 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): class TestInlineMedia(TestCase):
fixtures = ['admin-views-users.xml'] fixtures = ['admin-views-users.xml']