如何在 Django 管理站点上启用内联 ManyToManyFields?
假设我有书籍和作者模型。
class Author(models.Model):
name = CharField(max_length=100)
class Book(models.Model):
title = CharField(max_length=250)
authors = ManyToManyField(Author)
我希望每本书都有多个作者,并且在 Django 管理站点上,我希望能够从其编辑页面一次性将多个新作者添加到一本书中。我不需要向作者添加书籍。
这可能吗?如果是这样,实现它的最佳和/或最简单的方法是什么?
Let's say I have Books and Author models.
class Author(models.Model):
name = CharField(max_length=100)
class Book(models.Model):
title = CharField(max_length=250)
authors = ManyToManyField(Author)
I want each Book to have multiple Authors, and on the Django admin site I want to be able to add multiple new authors to a book from its Edit page, in one go. I don't need to add Books to authors.
Is this possible? If so, what's the best and / or easiest way of accomplishing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
做你想做的事情非常简单,如果我没理解错的话:
你应该在 apps 目录中创建一个 admin.py 文件,然后编写以下代码:
It is quite simple to do what you want, If I am getting you correctly:
You should create an admin.py file inside your apps directory and then write the following code:
试试这个:
如果您有很多作者,您可能需要将
raw_id_fields = ("author", )
添加到AuthorInline
中。Try this:
You might need to add
raw_id_fields = ("author", )
toAuthorInline
if you have many authors.好吧,看看 Django有关内联多对多用法的文档。
Well, check out the Django docs on many to many usage with inlines.