我有两个模型,有 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 的新手
谢谢 :)