Opened 6 years ago

Closed 5 years ago

Last modified 4 years ago

#12481 closed (fixed)

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

Reported by: lashni Owned by: nobody
Component: contrib.admin Version: 1.2-beta
Severity: Keywords:
Cc: jezdez 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 russellm)

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 lashni 6 years ago.
django-admin-readonly-editable.diff (2.1 KB) - added by Alex 5 years ago.
Tests + fix.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 6 years ago by kmtracey

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

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 6 years ago by lashni

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 6 years ago by Alex

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

comment:4 Changed 6 years ago by lashni

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 6 years ago by jezdez

  • Cc jezdez added

Changed 6 years ago by lashni

comment:6 Changed 6 years ago by lashni

  • Has patch set

comment:7 Changed 6 years ago by lashni

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

  • Description modified (diff)
  • milestone set to 1.2
  • Summary changed from admin readonly_fields not working with fieldsets to admin readonly_fields not working with edtiable=False field named in fieldset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from SVN to 1.2-beta

Updated ticket title and description to reflect the actual problem

Changed 5 years ago by Alex

Tests + fix.

comment:9 Changed 5 years ago by Alex

  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 5 years ago by kmtracey

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

(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 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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