【发布时间】:2017-07-11 19:22:29
【问题描述】:
我有两个模型,有 Book 和 Author,我在 Book 模型中添加了 ManyToMany 字段
class Author(models.Model):
name = models.CharField(verbose_name='name', max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.name)
class Book(models.Model):
title = models.CharField(verbose_name='title', max_length=50)
authors = models.ManyToManyField(Author) # Many to many
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.title)
如果我想从书籍和访问作者创建 CreateView,我可以添加这样的代码
class BookCreateView(CreateView):
model = Book
template_name = "books/book_create.html"
fields = ['title', 'authors']
但我想问的是我想从 Author 模型创建 CreateView 并在其中添加名为 books 的字段。我试着像这样写代码
class AuthorCreateView(CreateView):
model = Author
template_name = "books/author_create.html"
fields = ['name', 'books']
并显示错误“为作者指定的未知字段(书籍)”。
各位高手帮帮我,我是 django 新手
谢谢:)
【问题讨论】:
标签: python django many-to-many create-view