Django admin Inline Model Admin required
Having a such model开发者_如何学JAVA below, how can I require atleast one book to be added while creating/editting Author instance at admin panel?
#models.py
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
#admin.py
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
Matthew Flanagan has a formset class that will do just that: http://wadofstuff.blogspot.com/2009/08/requiring-at-least-one-inline-formset.html
Hope that helps you out.
精彩评论