Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#12481 closed (fixed)

admin readonly_fields not working with edtiable=False field named in fieldset

Reported by: Tim Miller Owned by: nobody
Component: contrib.admin Version: 1.2-beta
Severity: Keywords:
Cc: Jannis Leidel Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Russell Keith-Magee)

If a field is marked editable=False, and it is included in a fieldset in a model admin, and it is also listed in readonly_fields, an error is raised:

'PostAdmin.fieldsets[1][1]['fields']' refers to field 'date_modified' that is missing from the form.

Attachments (2)

rofv1.diff (854 bytes) - added by Tim Miller 7 years ago.
django-admin-readonly-editable.diff (2.1 KB) - added by Alex Gaynor 7 years ago.
Tests + fix.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 7 years ago by Karen Tracey

Resolution: worksforme
Status: newclosed

I can't recreate any problem along the lines you sketch out. I just added readonly_fields to one of my ModelAdmin classes that has fieldsets specified, listing several of the fields specified in fieldsets, and it works fine. They display and are not editable; no error. r12063 if it matters.

comment:2 Changed 7 years ago by Tim Miller

It looks like I was misusing readonly_fields then. I was using it to display fields that were set editable=False rather than using it on normal fields.

    date_created = models.DateField(auto_now_add=True, verbose_name='created')

That model field or any normal one to which I add editable=False will normally not display in an admin add or change form but if I list it in readonly_fields it does display. It also lets me position it with fields but not with fieldsets.

comment:3 Changed 7 years ago by Alex Gaynor

Using readonly_fields with editable=False should be supported. (I bet it actually works but the validation fails on it).

comment:4 Changed 7 years ago by Tim Miller

Resolution: worksforme
Status: closedreopened

I'm reopening the ticket, Alex is right in that it does actually display in admin when I comment out the relevant validation.

contrib/admin/validation.py

def check_formfield(cls, model, opts, label, field):
    if getattr(cls.form, 'base_fields', None):
        try:
            cls.form.base_fields[field]
        except KeyError:
            raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
                "is missing from the form." % (cls.__name__, label, field))
    else:
        fields = fields_for_model(model)
        try:
            fields[field]
        except KeyError:
            print "hit check_formfield keyerror #2"
            #raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
            #    "is missing from the form." % (cls.__name__, label, field))

comment:5 Changed 7 years ago by Jannis Leidel

Cc: Jannis Leidel added

Changed 7 years ago by Tim Miller

Attachment: rofv1.diff added

comment:6 Changed 7 years ago by Tim Miller

Has patch: set

comment:7 Changed 7 years ago by Tim Miller

Related problem with readonly_fields, I'm still trying to work out if it's the app or django yet. Using django-reversion to rollback to a previous model state fails with:

Caught an exception while rendering: Key 'date_created' not found in Form

traceback: http://dpaste.com/hold/140812/

comment:8 Changed 7 years ago by Russell Keith-Magee

Description: modified (diff)
milestone: 1.2
Summary: admin readonly_fields not working with fieldsetsadmin readonly_fields not working with edtiable=False field named in fieldset
Triage Stage: UnreviewedAccepted
Version: SVN1.2-beta

Updated ticket title and description to reflect the actual problem

Changed 7 years ago by Alex Gaynor

Tests + fix.

comment:9 Changed 7 years ago by Alex Gaynor

Triage Stage: AcceptedReady for checkin

comment:10 Changed 7 years ago by Karen Tracey

Resolution: fixed
Status: reopenedclosed

(In [12730]) Fixed #12481: Updated admin validation code to not reject non-editable fields in readonly_fields, since these are handled fine by the display code itself. Thanks lashni and Alex.

comment:11 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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