Opened 15 years ago

Closed 15 years ago

Last modified 5 years ago

#13233 closed Uncategorized (worksforme)

readonly_fields doesn't work with fields not part of the model

Reported by: benc Owned by: nobody
Component: contrib.admin Version: 1.2-beta
Severity: Normal Keywords: admin, readonly_fields
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

using readonly_fields in the admin works for fields of the model
but when I add a field to the form and try to make it readonly I'm getting an error:

ImproperlyConfigured at /admin/myapp/mymodel/1/
MyModelAdmin.readonly_fields[0], 'somefield' is not a callable or an attribute of 'MyModelAdmin' or found in the model 'MyModel'.

readonly_fields is useful to show calculated fields.
I'm using it to show the sum of inline cart items.

class MyModelAdminForm(forms.ModelForm):
    class Meta:
        model = MyModel

    somefield = forms.CharField()

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm    
    readonly_fields = ('somefield',)

Thanks

Change History (5)

comment:1 by Russell Keith-Magee, 15 years ago

Resolution: wontfix
Status: newclosed

I'm afraid I don't see why this should be possible. What data is going to populate the field when it is marked readonly?

If you want to display 'extra' data, then do exactly what the error message says - make it an attribute or callable on MyModelAdmin or MyModel -- see the docs for list_display for details on how to do this.

comment:2 by benc, 15 years ago

Resolution: wontfix
Status: closedreopened

Using an attribute or callable on MyModelAdmin or MyModel doesn't work.

I'm getting:

'MyModelAdmin.fieldsets[0][1]['fields']' refers to field 'somefield' that is missing from the form.

I think that list_display implements special functionality to use an attribute or callable that is missing from readonly_fields.

class MyModelAdminForm(forms.ModelForm):
    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm    
    readonly_fields = ('somefield',)

    def somefield(self, obj):
        return 'somefield test'

    fieldsets = (
            (None, {
                'fields': ('somefield',)
            })
    )

comment:3 by Russell Keith-Magee, 15 years ago

Resolution: worksforme
Status: reopenedclosed

Using r12896, I can't reproduce. The admin definition you provide works fine for me, displaying a single readonly row when editing instance of MyModel.

comment:4 by shadfc, 13 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

I am getting the error posted in the ticket description as well. My use case is creating a UserProfile form which also has the 'username' field so that I can offer a single form to create/edit user-userprofile combinations. When creating, the username field should act normally. I do not want users to be able to change their username, so when editing I want to set the 'username' field to readonly. Doing this via readonly_fields or get_readonly_fields throws an error similar to the one in the ticket that 'username' is not a callable or attribute of the modeladmin and is not found on the model.

I am on the latest 1.3 release.

class AdminProfileForm(forms.ModelForm):
    username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$')

    class Meta:
        model = UserProfile

    def __init__(self, *args, **kwargs):
        if 'instance' in kwargs:
            instance = kwargs['instance']
            user = instance.user
            if 'initial' in kwargs:
                kwargs['initial']['username'] = user.username
            else:
                kwargs['initial'] = {'username':user.username}
        super(AdminProfileForm, self).__init__(*args, **kwargs)
Last edited 13 years ago by shadfc (previous) (diff)

comment:5 by Baptiste Mispelon, 5 years ago

(I'm referring to a deleted comment)
Please don't change the description of tickets if you have questions. Especially for an old ticket like this, it's more appropriate to open a new ticket. Thanks.

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