基于对象的orm跨表查询再练习

model.py

from django.db import models


# Create your models here.

class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64)
    publishDate = models.DateField()
    price = models.DecimalField(max_digits=5, decimal_places=2)

    publish = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE)

    authors = models.ManyToManyField(to="Author")


class Author(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    # 作者和作者信息一对一
    AuthorDetail=models.OneToOneField(to="AuthorDetail",on_delete=models.CASCADE)


class AuthorDetail(models.Model):
    nid = models.AutoField(primary_key=True)
    # birthday = models.DateField()  如果报错 Python int too large to convert to C long 则注销这行
    tetephone = models.BigIntegerField()
    addr = models.CharField(max_length=64)


class Publish(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=32)
    email = models.EmailField()

views.py

from django.shortcuts import render

# Create your views here.
from django.shortcuts import HttpResponse, redirect, render
from .models import Book, Author, AuthorDetail, Publish


def index(request):

    ''' 一(出版社)对多(书籍) 查询 '''

    # 1、(正向查询按字段) 查询主键为1的书籍的出版社所在的城市
    book_obj=Book.objects.get(pk=1)
    print(book_obj.publish.city)

    # 2、(反向查询)查询 苹果出版社 出版的所有书籍的名字(反向查询按表名小写)
    #  “要查询的表_set”  就是反向查询所要查询的关联表数据
    publish_obj=Publish.objects.get(name="苹果出版社")
    print(publish_obj.city)
    book_list=publish_obj.book_set.all()
    print(book_list)
    for item in book_list:
        print(item.title)

    '''  一(作者)对一(作者详情)查询 '''
    # 1、反向查询  查询所有住址在北京的作者的姓名(通过authordetail去查询author表)
    author_detail_list=AuthorDetail.objects.filter(addr="北京").all()
    print(author_detail_list)
    for item in author_detail_list:
        print(item.author.name)

    # 2、正向查询 查询作者为tom的地址在哪里?
    author_obj=Author.objects.get(name="tom")
    print(author_obj) # Author object (1)
    print(author_obj.AuthorDetail)  # AuthorDetail object (1)
    print(author_obj.AuthorDetail.addr) # 北京

    '''多(Author)对多(Book)查询'''
    # 正向查询
    # 1、追风筝的人  所有作者的名字以及手机号
    book_obj=Book.objects.get(title="追风筝的人")
    print(book_obj) # Book object (3)
    author_list=book_obj.authors.all()
    print(author_list)  # <QuerySet [<Author: Author object (1)>, <Author: Author object (2)>]>
    for author_obj in author_list:
        print(author_obj) # Author object
        print(author_obj.name)
        print(author_obj.AuthorDetail.addr) # 再跨一张authordetail表

    # 2、反向查询 查询tom出过的所有书籍的名字
    author_obj=Author.objects.get(name="tom")
    print(author_obj) # Author object (1)
    book_list=author_obj.book_set.all()
    print(book_list) # <QuerySet [<Book: Book object (3)>, <Book: Book object (1)>]>
    for book_obj in book_list:
        print(book_obj.title)



    return HttpResponse("ok")
posted @ 2019-12-20 15:36  XuMou  阅读(193)  评论(0编辑  收藏  举报