django ORM查询

一对多查询

book表和publish表
关联属性(forginkey) 在Book表中

Book---->Publish 正向查询 book_obj.publish 按字段

Publish---->Book 反向查询 publish_obj.book_set.all() 按表名小写_set

eg:

book表

idtitlepricepublish
1西游记200.001
2三国演义300.002
3水浒传200.001
4红楼梦240.002

publish表

idpublish_namepublish_email
1人民出版社
2北京出版社

# 正向查询,查询西游记的出版社名字

book_obj = Book.object.filter(title=‘西游记‘).first()
print(book_obj.publish_name)
print(bool_obj.publish_email)


# 反向查询,查询人民出版社的出版过的书
publish_obj = Publish.objects.filter(publish=‘人民出版社‘).first()
ret = publish_obj.book_set.all()

相关推荐