nalzok
nalzok

Reputation: 16127

Inline listing shows redundant items in Django Admin

Let me show you an example:

admin.py

from django.contrib import admin

from .models import Author, Book


class BookInline(admin.TabularInline):
    model = Book

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'date_of_birth', 'date_of_death')
    fields = ['first_name', 'last_name', ('date_of_birth', 'date_of_death')]
    inlines = [BookInline]

This configuration generates the following layout:

enter image description here

The problem is, this author has only written one book(i.e. Book 009), but I got four books listed out(i.e. Book 009 and three "empty" books). Why is it? How can I disable this feature(or bug, whatever)?

enter image description here

Upvotes: 1

Views: 115

Answers (1)

nik_m
nik_m

Reputation: 12096

Django admin adds additional rows in case you want to add values for each of them.

You can control how many extra appear by setting the relevant attribute, like this:

class BookInline(admin.TabularInline):
    model = Book
    extra = 1  # or extra = 0

There also other goodies for your inline controls like, max_num, min_num etc.

Upvotes: 5

Related Questions