开发者

Django admin Inline Model Admin required

开发者 https://www.devze.com 2023-03-28 09:03 出处:网络
Having a such model开发者_如何学JAVA below, how can I require atleast one book to be added while creating/editting Author instance at admin panel?

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

精彩评论

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