I have the following model classes:
class Category(models.Model):
category = models.CharField('category', max_length=200, blank=False)
class Book(models.Model):
title = models.CharField('title', max_length=200, blank=False)
categories = models.ManyToManyField(Category, blank=False, through='Book_Category')
class Book_Category(models.Model):
book = models.ForeignKey(Book)
category = models.ForeignKey(Category)
When adding a new book object in admin interface I would like to also add a new category, and book_category relationship.
If I include categories in BookAdmin as
class BookAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['categories', ]}), ...
I get can't include the ManyToManyField field 'categories' because 'categories' manually specifies a 'through' model
error.
Is there any way to achieve required functionality?
class CategoryInline(admin.TabularInline):
model = Book_Category
extra = 3 # choose any number
class BookAdmin(admin.ModelAdmin):
inlines = (CategoryInline,)
admin.site.register(Book, BookAdmin)
Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With