Opened 5 years ago

Closed 5 years ago

Last modified 4 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 (4)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

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 Changed 5 years ago by benc

  • Resolution wontfix deleted
  • Status changed from closed to reopened

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 Changed 5 years ago by russellm

  • Resolution set to worksforme
  • Status changed from reopened to closed

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 Changed 4 years ago by shadfc

  • Easy pickings unset
  • Severity set to Normal
  • Type set to 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 4 years ago by shadfc (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top