我对 django 模型 sql 插入/更新有问题。我正在阅读官方教程,在第 5 章中有一个简单的数据库,其中包含 Authors、Books 和 Publisher 表。Author 表有 3 个字段:first_name、last_name、email Book 表也有一些字段,例如:name、publisher 等,以及与 Author 表具有多对多关系的 authors 字段。现在我正在尝试手动执行 django 管理应用程序在幕后所做的事情。我想添加或更新与给定书籍关联的作者。
我是这样开始的(在 shell 阶段):
from mysite.models import Book, Author
new_author1 = 'John Doe' # that first_name and last_name exists in Author table
new_author2 = 'Jane Doe' # that first_name and last_name exists in Author table
b = Book.objects.get(pk=2) # a book with id 2 exists in a Book table
b.authors = (new_author1,new_author2) # ?? trying to add/associate authors names with a selected book (pk=2)
b.save()
这当然行不通,我不知道我错过了什么