Cerin
Cerin

Reputation: 64911

Django Admin TabularInline Complaining About Missing Field

I have the following model and TabularInline subclasses:

class SomeModel(models.Model):
    name = models.CharField(max_length=50)

class SomeModelInline(admin.TabularInline):
    model = SomeModel

class SomeOtherModelAdmin(admin.ModelAdmin):
    inlines = [SomeModelInline]

Without explicitly specifying the TabularInline's fields, Django's admin shows the fields "id" and "name". However, when I try and do:

class SomeModelInline(admin.TabularInline):
    model = SomeModel
    fields  ['id','name']

Django throws the ImproperlyConfigured exception:

'SomeModelInline.fields' refers to field 'id' that is missing from the form.

What's going on here? Why can't I explicitly specify the id, even though Django's clearly capable of accessing it?

Upvotes: 2

Views: 1717

Answers (1)

Sergey Golovchenko
Sergey Golovchenko

Reputation: 18681

Ids are non-editable, by default inline shows the editable fields, but you can show the non-editable fields as well

From django docs

fields can contain values defined in ModelAdmin.readonly_fields to be displayed as read-only.

So first add 'id' to readonly_fields, then add it to fields

Upvotes: 4

Related Questions