Opened 19 months ago

Closed 2 months ago

#29682 closed Bug (fixed)

Admin change form crashes if a view-only model's form has field not on the model

Reported by: Jones Owned by: nobody
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 (last modified by Tim Graham)

Viewing an existing view-only object crashes with:

AttributeError at /admin/t29682/modela/1/change/
Unable to lookup 'form_field' on ModelA or ModelAAdmin
class ModelAForm(forms.ModelForm):

    class Meta:
        model = ModelA
        fields = '__all__'

    form_field = forms.BooleanField(
        label="Form Field",
        required=False,
        widget=forms.CheckboxInput()
    )


class ModelA(models.Model):
    test = models.IntegerField(
        null=True,
        blank=True
    )

@admin.register(ModelA)
class ModelAAdmin(admin.ModelAdmin):

    form = ModelAForm

    fieldsets = (
        ('Test', {
            'fields': (
                'test', 'form_field'
            )
        }),
    )

Change History (7)

comment:1 Changed 19 months ago by Tim Graham

Component: Formscontrib.admin
Description: modified (diff)
Severity: NormalRelease blocker
Summary: Error in view permission with custom model form fieldAdmin change form crashes if a view-only model's form has field not on the model
Triage Stage: UnreviewedAccepted

comment:2 Changed 19 months ago by Tim Graham

Has patch: set

comment:3 Changed 18 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In d311124:

Fixed #29682 -- Fixed admin change form crash if a view-only model's form has an extra field.

comment:4 Changed 18 months ago by Tim Graham <timograham@…>

In 53b9a665:

[2.1.x] Fixed #29682 -- Fixed admin change form crash if a view-only model's form has an extra field.

Backport of d311124be59df64278f3149d68e79ce45b8a6c64 from master

comment:5 Changed 16 months ago by Jones

Has another problem:

If I have set this field readonly.

@admin.register(ModelA)
class ModelAAdmin(admin.ModelAdmin):

    form = ModelAForm

    readonly_fields = ('form_field', )

    fieldsets = (
        ('Test', {
            'fields': (
                'test', 'form_field'
            )
        }),
    )
Last edited 16 months ago by Jones (previous) (diff)

comment:6 Changed 2 months ago by fu2re

Resolution: fixed
Status: closednew
Version: 2.12.2

Issue is still persist if a field is readonly as described above.

comment:7 Changed 2 months ago by felixxm

Resolution: fixed
Status: newclosed
Version: 2.22.1

As far as I'm concerned a custom form field cannot be in the readonly_fields, e.g.

<class 'admin_views.admin.ArticleAdmin'>: (admin.E035) The value of 'readonly_fields[0]' is not a callable, an attribute of 'ArticleAdmin', or an attribute of 'admin_views.Article'.

Please open a new ticket with a sample project if you believe that there is any issue with readonly_fields.

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