For the following code:
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
class BookInline(admin.TabularInline):
model = Book
extra = 1
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
If I add a new Author through the admin, it will show me one Book entry because of the extra=1
. If I edit an existing Author who has one Book, the admin will show the existing book and a new field to add a new one.
Current behaviour with Edit an Author who has 1 book:
Author: Someone
Book: The book title
Book #2:
Wanted behaviour with Edit an Author who has 1 book:
Author: Someone
Book: The book title
Is it possible in the admin.py
for the above code to check whether I'm in the add or edit page? My goal is to set extra=1
for add and extra=0
for edit.