Opened 4 years ago

Closed 2 years ago

#15968 closed Cleanup/optimization (fixed)

readonly_fields ignore form field label

Reported by: gabomdq@… Owned by: nobody
Component: Documentation Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


class MyForm(ModelForm):
    test_field_callable = CharField(label=_('This is a test label'))

class MyAdmin(ModelAdmin)
    form = MyForm

class MyModel(Model)
    def test_field_callable(self):
        print 'test content'

I'm using the previous pseudo code as a guide to explain what happens, it's not a complete test case. If the field test_field_callable is not listed on readonly_fields, I get the proper label displayed on the Admin. However, with the readonly_fields listing test_field_callable, the label printed is "Test Field Callable" (a beautified version of the field name instead of the label I specified on the form).

Attachments (1)

15968-testcase.patch (1.4 KB) - added by aaugustin 4 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 4 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I could reproduce the issue, I'm attaching a failing test case.

If comments is removed from readonly_fields in PizzaAdmin, the test passes.

Changed 4 years ago by aaugustin

comment:2 Changed 4 years ago by julien

  • Component changed from contrib.admin to Documentation

This is actually the normal behaviour. If you declare a field as read-only, then that field will be excluded from the form altogether. I agree this may be confusing though, so the admin doc could be clarified.

comment:3 Changed 4 years ago by anonymous

If that is so, what is the recommended way to set a custom label in this case?

comment:4 Changed 4 years ago by julien

contrib.admin.helpers.AdminReadonlyField is where you'll find the details of how the read-only fields are built. It is fairly inflexible and there's no API for controlling much about it. Yet :-)

In the meantime, a work-around is to provide a verbose_name to your model field.

comment:5 Changed 4 years ago by anonymous

Thanks for the tip, I've already been down that road and gone through the code throughly before reporting here, but you'll have to agree that what you comment does not apply to this case. There's no verbose_name to be set because the field is not really part of the Model, it's just a method. That's why I don't think it's a documentation issue, if it is not considered a bug (which I believe to be the case), it's a Feature Request.

comment:6 Changed 4 years ago by julien

Yes you're right about verbose_name, I read your code sample a bit too fast. The feature you're after already exists with short_description:

class MyModel(models.Model):
    name = models.CharField(max_length=100, blank=True)
    def test_field_callable(self):
        print 'test content'
    test_field_callable.short_description = 'blah'

comment:7 Changed 4 years ago by julien

  • Type changed from Bug to Cleanup/optimization

Just to synthesise, there is no bug as this is the normal behaviour, and the feature you were after already exists. The documentation about read-only fields is a bit succinct and it could be improved in a few ways, at least by emphasising that form fields are excluded from the ModelForm if declared as readon-only, and by providing a tip on modifying the label of a read-only "field" if that "field" is a callable.

comment:8 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:9 Changed 2 years ago by timo

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top