如果有这样的模型,在管理面板上创建/编辑作者实例时,如何才能要求添加至少一本书?
#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,
]
答案 0 :(得分:0)
Matthew Flanagan有一个formset类,可以做到这一点:http://wadofstuff.blogspot.com/2009/08/requiring-at-least-one-inline-formset.html
希望能帮到你。