Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#13174 closed (fixed)

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

Reported by: benc Owned by: gwilson
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: UI/UX:

Description

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: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

  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.
models.py:
class Author(models.Model):
   name = models.CharField(max_length=100)

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

admin.py:
class BookInline(admin.StackedInline):
    model = Book
    readonly_fields = ('title',)

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

admin.site.register(Author, AuthorAdmin)

Attachments (1)

12843.diff (1003 bytes) - added by ptone 4 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 years ago by benc

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by russellm

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

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

comment:3 Changed 4 years ago by ptone

  • 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 4 years ago by ptone

comment:4 Changed 4 years ago by anonymous

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

comment:5 Changed 4 years ago by gwilson

  • Owner changed from anonymous to gwilson
  • Status changed from assigned to new

comment:6 Changed 4 years ago by gwilson

  • Resolution set to fixed
  • Status changed from new to closed

(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 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.