import os
if __name__ == '__main__':
#加载Django项目的配置信息
os.environ.setdefault("DJANGO_SETTINGS_MODULE","testinclude.settings")
#导入Django,并启动Django项目
import django
django.setup()
from ormtest import models
#正向查询(查找书的类型对象,然后通过外键关联,在出版社列中查出那家出版社)
book_obj = models.Book.objects.first()
book_obj_publisher = book_obj.publisher.name
print("书名:",book_obj,"出版社为:",book_obj_publisher)
#反向查询
publisher_obj = models.Publisher.objects.first()
print(publisher_obj)
publisher_obj_book = publisher_obj.books.all()
#books为book类中定义的related_name
print(publisher_obj_book)
书名: book_title:网络基础原理 出版社为: 东北大学出版社
publisher_name:清华大学出版社
<QuerySet [<Book: book_title:Linux基础>, <Book: book_title:计算机原理>, <Book: book_title:Django从入门到精通>]>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。