Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#13174 closed (fixed)

readonly_fields in StackedInline forms in the admin are missing the field label.

Reported by: benc Owned by: Gary Wilson
Component: contrib.admin Version: 1.2-beta
Severity: Keywords: admin, forms, inline, label
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


readonly_field in StackedInline forms in the admin are missing the field label.

The field value is shown but the label is missing.

The label of readonly_field in normal forms is ok.

I'm using the example from the docs about inline forms in the admin:

  1. in the admin create an Author when readonly_fields is commented out so you can create a book and a title.
  1. remove the comment from readonly_fields from BookInline and see that the Book title exists but the label is missing.
class Author(models.Model):
   name = models.CharField(max_length=100)

class Book(models.Model):
   author = models.ForeignKey(Author)
   title = models.CharField(max_length=100)
class BookInline(admin.StackedInline):
    model = Book
    readonly_fields = ('title',)

class AuthorAdmin(admin.ModelAdmin):
    inlines = (BookInline,), AuthorAdmin)

Attachments (1)

12843.diff (1003 bytes) - added by Preston Holmes 9 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 9 years ago by benc

comment:2 Changed 9 years ago by Russell Keith-Magee

milestone: 1.2
Triage Stage: UnreviewedAccepted

This is only a problem with StackedInlines; TabularInlines work as expected.

comment:3 Changed 9 years ago by Preston Holmes

Has patch: set

There were two issues here. First in the init of InlineAdminForm, the model_admin param was not being passed on to the call to the superclass (AdminForm) and so that attribute was getting reset to none. This was screwing up the function at admin.helpers.AdminReadonlyField.label_tag which was expecting model_admin.model to be available.

However even when fixing this, it seems that what is wanted for the label is not something based on the parent model (which is what is found in this context in model_admin.model) but the field name itself, which is available in the local for the label_tag function.

The attached diff fixes these, and in the ad_hoc test as recreated from the report seems to fix things.

Changed 9 years ago by Preston Holmes

Attachment: 12843.diff added

comment:4 Changed 9 years ago by anonymous

Owner: changed from nobody to anonymous
Status: newassigned

comment:5 Changed 9 years ago by Gary Wilson

Owner: changed from anonymous to Gary Wilson
Status: assignednew

comment:6 Changed 9 years ago by Gary Wilson

Resolution: fixed
Status: newclosed

(In [12857]) Fixed #13174 -- Fixed missing field label for readonly_fields when used in StackedInline, thanks to benc for the report and ptone for the investigation and initial patch.

  • Corrected InlineAdminForm.__init__ to pass along model_admin parameter in super call.
  • Lookup the field label in the form's model, not the model_admin model.

comment:7 Changed 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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