【发布时间】:2012-12-01 10:15:05
【问题描述】:
我对 django 模型 sql 插入/更新有疑问。 我正在阅读官方教程,在第 5 章中,有一个带有作者的简单数据库, 书籍和出版商表。 Author 表有 3 个字段:first_name、last_name、email Book 表也有一些字段,例如:名称、出版商等,以及多对多的作者字段 与 Author 表的关系。现在我正在尝试手动执行,django admin app 正在做什么 在幕后。我想添加或更新与给定图书关联的作者。
我是这样开始的(在 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()
这当然行不通,我不知道我错过了什么
【问题讨论】:
标签: python django django-models