Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#29929 closed Bug (fixed)

ModelAdmin.prepopulated_fields crashing admin when showing read-only view

Reported by: Sébastiaan Versteeg Owned by: Basil Dubyk
Component: contrib.admin Version: 2.1
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The values of the ModelAdmin.prepopulated_fields field will be used when showing a read-only view in the admin. This results in a KeyError: "Key 'to_be_prepopulated' not found in 'SampleModelForm'. Choices are: .".

class SampleModel(models.Model):
    input_value = models.TextField()
    to_be_prepopulated = models.TextField()
@admin.register(SampleModel)
class SampleModelAdmin(admin.ModelAdmin):
    prepopulated_fields = {'to_be_prepopulated': ('input_value',)}

    def has_change_permission(self, request, obj=None):
        return False

Attachments (1)

0001-Tests-that-demonstrate-breakage.patch (4.1 KB ) - added by Thom Wiggers 6 years ago.
The attached file demonstrates the issue in the Django test suite.

Download all attachments as: .zip

Change History (9)

by Thom Wiggers, 6 years ago

The attached file demonstrates the issue in the Django test suite.

comment:2 by Thom Wiggers, 6 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Carlton Gibson, 6 years ago

Severity: NormalRelease blocker

comment:4 by Sébastiaan Versteeg, 6 years ago

Would it suffice to filter all read-only fields from the prepopulated fields? That would also have the side effect that normal read-only fields are also allowed to be prepopulated though.

comment:5 by Basil Dubyk, 6 years ago

Owner: changed from nobody to Basil Dubyk
Status: newassigned

comment:6 by Basil Dubyk, 6 years ago

Has patch: set

comment:7 by Tim Graham <timograham@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 7d1123e5:

Fixed #29929 -- Fixed admin view-only change form crash when using ModelAdmin.prepopulated_fields.

comment:8 by Tim Graham <timograham@…>, 5 years ago

In 381bdd48:

[2.1.x] Fixed #29929 -- Fixed admin view-only change form crash when using ModelAdmin.prepopulated_fields.

Backport of 7d1123e5ada60963ba3c708a8932e57342278706 from master.

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