Opened 6 years ago

Closed 5 years 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 by Tim Graham, 6 years ago

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 by Tim Graham, 6 years ago

Has patch: set

comment:3 by Tim Graham <timograham@…>, 6 years ago

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 by Tim Graham <timograham@…>, 6 years ago

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

Has another problem:

If I have set this field with readonly.

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

    form = ModelAForm

    readonly_fields = ('form_field', )

    fieldsets = (
        ('Test', {
            'fields': (
                'test', 'form_field'
            )
        }),
    )
Version 0, edited 6 years ago by Jones (next)

comment:6 by fu2re, 5 years ago

Resolution: fixed
Status: closednew
Version: 2.12.2

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

comment:7 by Mariusz Felisiak, 5 years ago

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