Opened 6 years ago

Closed 6 years ago

#29676 closed Bug (duplicate)

has_add_permission() returning False in TabularInline form raises exception in contrib.admin

Reported by: Ivan Belokobylskiy Owned by: nobody
Component: contrib.admin Version: 2.1
Severity: Normal Keywords: admin has_add_permission
Cc: Ivan Belokobylskiy Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have TabularInline form does not have add permission and not all fields are read-only.
Its model has a FK to parent model without any restrictions.
When I try to create a new parent model via contrib.admin form KeyError exception raised.

This example application fails on Django 2.1, Python 3.6 when I try to visit
http://localhost:8000/admin/app/parentmodel/add/
It raises KeyError: 'editable_field'

models.py

from django.db import models


class ParentModel(models.Model):
    name = models.CharField(max_length=255)


class InlineModel(models.Model):
    editable_field = models.CharField(max_length=255)
    readonly_field = models.CharField(max_length=255)
    parent = models.ForeignKey(ParentModel, on_delete=models.CASCADE)

admin.py

from django.contrib import admin

from .models import InlineModel, ParentModel


class InlineModelInline(admin.TabularInline):
    model = InlineModel
    fields = (
        'editable_field',
        'readonly_field',
    )
    readonly_fields = (
        'readonly_field',
    )

    def has_add_permission(self, request, obj):
        return False


@admin.register(ParentModel)
class ParentModelAdmin(admin.ModelAdmin):
    fields = ['name']
    inlines = [InlineModelInline]

Change History (2)

comment:1 by Ivan Belokobylskiy, 6 years ago

Cc: Ivan Belokobylskiy added

comment:2 by Tim Graham, 6 years ago

Resolution: duplicate
Status: newclosed

It looks like a duplicate of #29637. It'll be fixed in 2.1.1, but feel free to test with the stable/2.1.x branch to confirm.

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