Opened 12 months ago

Closed 11 months ago

Last modified 11 months 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 12 months ago.
The attached file demonstrates the issue in the Django test suite.

Download all attachments as: .zip

Change History (9)

Changed 12 months ago by Thom Wiggers

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

comment:2 Changed 12 months ago by Thom Wiggers

Triage Stage: UnreviewedAccepted

comment:3 Changed 12 months ago by Carlton Gibson

Severity: NormalRelease blocker

comment:4 Changed 12 months ago by Sébastiaan Versteeg

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 Changed 12 months ago by Basil Dubyk

Owner: changed from nobody to Basil Dubyk
Status: newassigned

comment:6 Changed 12 months ago by Basil Dubyk

Has patch: set

comment:7 Changed 11 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 7d1123e5:

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

comment:8 Changed 11 months ago by Tim Graham <timograham@…>

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