3、多对多
- 正向:
对象.属性(book.authors)
可以跳转到关联的表 - 下面寻找Python教程的所有作者以及手机号,即正向,由于作者表里并没有手机号,所以还需要通过
对象.属性(i.au_detail)
跳转到关联的表
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") book = models.Book.objects.filter(title="Python教程").first() res = book.authors.all() for i in res: print(i.name,i.au_detail.tel) return HttpResponse("ok")
- 访问测试
- 查看终端输出和数据库信息
- 下面查询任我行写的所有书籍的名称,即反向
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") author = models.Author.objects.filter(name="任我行").first() res = author.book_set.all() for i in res: print(i.title) return HttpResponse("ok")
- 访问测试
- 查看终端输出和数据库信息
五、基于双下划线的跨表查询
- 正向:
属性名称__跨表的属性名称
- 反向:
小写类名__跨表的属性名称
1、一对多
- 查询华山出版社出版过的所有书籍名称和价格
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") res = models.Book.objects.filter(publish__name="华山出版社").values_list("title","price") return HttpResponse(res)
- 访问测试
查看数据库信息
- 下面是反向,通过
小写类名__跨表的属性名称(book__title,book__price)
跨表获取数据
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") res = models.Publish.objects.filter(name="华山出版社").values_list("book__title","book__price") return HttpResponse(res)
访问测试
2、多对多
- 查询任我行出过的所有书籍的名称
- 正向:通过
属性名称__跨表的属性名称(authors__name)
跨表获取数据:
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") res = models.Book.objects.filter(authors__name="任我行").values_list("title") return HttpResponse(res)
- 访问测试
反向:通过小写类名__跨表的属性名称(book__title)跨表获取数据
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") res = models.Author.objects.filter(name="任我行").values_list("book__title") return HttpResponse(res)
- 访问测试
3、一对一
- 查询任我行的手机号
- 正向:通过
**属性名称__跨表的属性名称(au_detail__tel)
跨表查询数据
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") res = models.Author.objects.filter(name="任我行").values_list("au_detail__tel") return HttpResponse(res)
- 访问测试
- 反向:通过
小写类名__跨表的属性名称(author__name)
跨表获取数据
# -*- coding: utf-8 -*- from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") res = models.AuthorDetail.objects.filter(author__name="任我行").values_list("tel") return HttpResponse(res)
访问测试