Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#16838 closed Bug (fixed)

First InlineModelAdmin not displaying "Add Another" link when related_name='+'

Reported by: jamesp Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The first InlineModelAdmin displayed in a ModelAdmin subclass does not show the "Add Another" link.

The admin HTML code that gets generated for inlines appears to be inconsistent (for example, the generated HTML has id="id_MAX_FORMS" while the inlines jQuery is looking for #id_-MAX_FORMS).

Attachments (3)

inlines-add_another-missing.png (16.3 KB ) - added by jamesp 13 years ago.
"Add another" link missing with related_name='+' in ForeignKey
t16838-inlines_tests.diff (3.8 KB ) - added by jamesp 13 years ago.
Tests to pass for admin inlines with related_name='+'
t16838-potential_fix.diff (6.1 KB ) - added by jamesp 13 years ago.
Potential fix for this bug

Download all attachments as: .zip

Change History (11)

comment:1 by Carl Meyer, 13 years ago

Resolution: worksforme
Status: newclosed

Using the following models.py and admin.py on trunk Django, I get "add another" links for both book and subject when I add/edit an author.

models.py:

from django.db import models


class Author(models.Model):
    name = models.CharField(max_length=100)


class Subject(models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(Author)


class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author)

admin.py:

from django.contrib import admin

from .models import Book, Author, Subject


class BookInline(admin.TabularInline):
    model = Book


class SubjectInline(admin.TabularInline):
    model = Subject


admin.site.register(Subject)
admin.site.register(Book)
admin.site.register(Author, inlines=[BookInline, SubjectInline])

comment:2 by jamesp, 13 years ago

Resolution: worksforme
Status: closedreopened

I got your results with the code above and realized I needed to provide more information about my models.

If you set up the ForeignKeys with related_name='+', then you get the behavior I described:

from django.db import models


class Author(models.Model):
    name = models.CharField(max_length=100)


class Subject(models.Model):
    name = models.CharField(max_length=100, related_name='+')
    author = models.ForeignKey(Author)


class Book(models.Model):
    title = models.CharField(max_length=100, related_name='+')
    author = models.ForeignKey(Author)

Version 0, edited 13 years ago by jamesp (next)

by jamesp, 13 years ago

"Add another" link missing with related_name='+' in ForeignKey

comment:3 by jamesp, 13 years ago

Summary: First InlineModelAdmin not displaying "Add Another" linkFirst InlineModelAdmin not displaying "Add Another" link when related_name='+'

comment:4 by Carl Meyer, 13 years ago

Triage Stage: UnreviewedAccepted

Verified. Note that related_name actually should be on the FKs, not the CharFields ;-) Thanks for the report!

comment:5 by Carl Meyer, 13 years ago

#13407 was a recent bug in the same area, likely related.

comment:6 by jamesp, 13 years ago

My god, I am a mess. You're right, it should be on the ForeignKeys.

I need some damn sleep. :P

by jamesp, 13 years ago

Attachment: t16838-inlines_tests.diff added

Tests to pass for admin inlines with related_name='+'

by jamesp, 13 years ago

Attachment: t16838-potential_fix.diff added

Potential fix for this bug

comment:7 by jamesp, 13 years ago

Has patch: set

What's occurring here is that related_name='+' causes the form prefix to be blank. This causes issues for inlines because the first item comes through with an entirely blank prefix. This behavior breaks the admin Javascript.

The attached patch passes the regression tests, as best I can tell.

comment:8 by Carl Meyer, 13 years ago

Resolution: fixed
Status: reopenedclosed

In [16860]:

Fixed #16838 -- Corrected broken add-another inline JS in admin with related_name="+". Thanks jamesp for report and patch.

Note: See TracTickets for help on using tickets.
Back to Top