zoukankan      html  css  js  c++  java
  • python-orm

    #########################基于对象查询(子查询)############################
                                       按字段(publish)
    1) 一对多 : book -----------------------------------> publish
                              <----------------------------------

                                     book_set.all()
    正向查询按字段:查询python这本书籍的出版社的邮箱
    python=models.Book.objects.filter(title="python").first()
    print(python.publish.email)
    '''
    对应sql:
    select publish_id from Book where title="python"
    select email from Publish where nid = 1
    ''’
    反向查询按 表名小写_set.all() : 苹果出版社出版的书籍名称
    publish_obj=models.Publish.objects.filter(name="苹果出版社").first()
    for obj in publish_obj.book_set.all():
    print(obj.title)

                                       按字段(authors.all())
    2) 多对多: book -------------------------------------> author
                                <-------------------------------------
                                      book_set.all()
    正向查询python作者的年龄:
    python = models.Book.objects.filter(title="python").first()
    for author in python.authors.all():
    print(author.name ,author.age)

    反向查询alex出版过的书籍名称:
    alex=models.Author.objects.filter(name="alex").first()
    for book in alex.book_set.all():
    print(book.title)

              按字段 authorDetail
    3) 一对一 author ----------------------------------------> authordetail
            <----------------------------------------
              按表名 author(不要_set)
    正向查询 alex的手机号:
    alex=models.Author.objects.filter(name='alex').first()
    print(alex.authorDetail.telephone)

    反向查询 家在山东的作者名字:
    author_obj =models.AuthorDetail.objects.filter(addr="shandong").first() # shandong只有一个
    print(author_obj.author.name)
    ad_list=models.AuthorDetail.objects.filter(addr="shandong") # shandong有多个
    for ad in ad_list:
    print(ad.author.name)


    ###################基于queryset和__查询(join查询)#####################

    查询python这本书籍的出版社的邮箱:
    正向查询: 按字段(关联字段)__xxx
    ret=models.Book.objects.filter(title="python").values("publish__email")
    print(ret.query)
    反向查询: 按表名小写__xxx
    ret=models.Publish.objects.filter(book__name="python").values("email")
    print(ret)
    '''SQL:
    select publish.email from Book
    left join Publish on book.publish_id=publish.nid
    where book.title="python"
    '''

    查询苹果出版社出版的书籍名称:
    正向查询:字段__xxxx
    ret2=models.Book.objects.filter(publish__name="苹果出版社").values("title")
    print("2222222222====>", ret2.query)
    反向查询:表名小写__xxx
    ret1=models.Publish.objects.filter(name="苹果出版社").values("book__title")
    print("111111111====>",ret1.query)


    查询alex的手机号
    正向查询:字段__xxxx
    ret=models.Author.objects.filter(name="alex").values("authorDetail__telephone")
    反向查询:表名小写__xxx
    models.AuthorDetail.objects.filter(author__name="alex").values("telephone")


    查询手机号以151开头的作者出版过的书籍名称以及书籍对应的出版社名称
    ret=models.Book.objects.filter(authors__authorDetail__telephone__startswith="151").values('title',"p ublish__name")
    print(ret.query)

    。。。。。。。。。。。。。。。。。。
  • 相关阅读:
    java算法
    2012 要找回曾经的忘我的激情
    啊啊啊
    利用ORACLE JOB 模拟多线程应用
    有没有一种数据库叫思科
    且行好事,莫问前程
    女人浪漫的好点子
    What is the difference between interface and abstract class
    优秀是一种习惯
    C#判断ContextMenuStrip右键菜单的来源(从哪个控件弹出来的) (转载)
  • 原文地址:https://www.cnblogs.com/fangsheng/p/9747323.html
Copyright © 2011-2022 走看看