Opened 13 years ago

Closed 11 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

Description

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

class MyAdmin(ModelAdmin)
    form = MyForm
    readonly_fields=['test_field_callable',]

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 Aymeric Augustin 13 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

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

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

by Aymeric Augustin, 13 years ago

Attachment: 15968-testcase.patch added

comment:2 by Julien Phalip, 13 years ago

Component: contrib.adminDocumentation

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 by anonymous, 13 years ago

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

comment:4 by Julien Phalip, 13 years ago

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 by anonymous, 13 years ago

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 by Julien Phalip, 13 years ago

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 by Julien Phalip, 13 years ago

Type: BugCleanup/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 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 by Tim Graham, 11 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top