开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜