Opened 5 years ago

Closed 5 years ago

Last modified 5 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: master
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 5 years ago.
"Add another" link missing with related_name='+' in ForeignKey
t16838-inlines_tests.diff (3.8 KB) - added by jamesp 5 years ago.
Tests to pass for admin inlines with related_name='+'
t16838-potential_fix.diff (6.1 KB) - added by jamesp 5 years ago.
Potential fix for this bug

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 years ago by Carl Meyer

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
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 Changed 5 years ago by jamesp

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)
    author = models.ForeignKey(Author, related_name='+')


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

Last edited 5 years ago by jamesp (previous) (diff)

Changed 5 years ago by jamesp

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

comment:3 Changed 5 years ago by jamesp

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

comment:4 Changed 5 years ago by Carl Meyer

Triage Stage: UnreviewedAccepted

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

comment:5 Changed 5 years ago by Carl Meyer

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

comment:6 Changed 5 years ago by jamesp

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

I need some damn sleep. :P

Changed 5 years ago by jamesp

Attachment: t16838-inlines_tests.diff added

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

Changed 5 years ago by jamesp

Attachment: t16838-potential_fix.diff added

Potential fix for this bug

comment:7 Changed 5 years ago by jamesp

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 Changed 5 years ago by Carl Meyer

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