码迷,mamicode.com
首页 > 其他好文 > 详细

orm

时间:2018-08-16 00:47:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:rom   ESS   nsa   less   去重   没有   avg   str   字段排序   

orm——13

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")
    import django

    django.setup()

    from app01 import models

    # all 查询所有数据  ——》QuerySet
    ret = models.Person.objects.all()
    # print(ret)

    # get 获取一个对象  如果查询没有或者多个  就报错
    ret = models.Person.objects.get(id=1)
    ret = models.Person.objects.get(name=和尚)
    # print(ret)

    # filter 查询所有满足条件的对象  ——》QuerySet
    ret = models.Person.objects.filter(id=1)
    ret = models.Person.objects.filter(name=杜举飞1)

    # exclude 查询所有不满足条件的对象  ——》QuerySet
    ret = models.Person.objects.exclude(id=1)

    # values 不写参数 取对象的所有字段数据  指定参数  取对象指定字段的参数 ——》QuerySet  元素是字典
    ret = models.Person.objects.values(name,age)
    # print(ret)

    # print(‘*‘*120)
    # for i in ret:
    #     print(i,type(i))

    # values_list
    # 不写参数 取对象的所有字段数据 元组形式
    # 指定参数 取对象指定字段的参数 ——》QuerySet  按照你参数顺序排序
    ret = models.Person.objects.values_list(age,name)
    # print(ret)
    #
    # print(‘*‘*120)
    # for i in ret:
    #     print(i,type(i))

    # order_by 按照指定字段排序 默认升序  加负号降序
    # 可以多字段排序
    ret =models.Person.objects.all().order_by(age,-id)
    # for i in ret:
    #     print(i,i.id,i.age)


    # reverse 对已经排序的QuerySet进行反向排序
    # 用了order_by  要不用了ordering
    ret =models.Person.objects.all().order_by(age,-id).reverse()
    # for i in ret:
    #       print(i,i.id,i.age)

    # ret = models.Person.objects.all()
    # print(ret)
    # ret = models.Person.objects.all().reverse()
    # print(ret)

    # distinct 去重


    # count() 计数  对QuerySet的对象进行计数
    ret = models.Person.objects.filter(id=100).count()

    # first 取QuerySet中的第一元素
    ret = models.Person.objects.all().first()


    ret = models.Person.objects.last()

    # print(ret)
    ret = models.Person.objects.filter(id=100).exists()
    print(ret)


"""
返回QuerySet的方法:
1. all()
2. filter()
3. exclude()
4. values()
5. values_list()
6. reverse()
7. distinct()
8. order_by()

返回具体对象的方法:
1. get()
2. first()
3. last()

返回数字的方法:
1. count()

返回布尔值:
1. exists()
"""

2双下划线

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")

    import django
    django.setup()

    from app01 import models
    ret = models.Person.objects.filter(id__gt=1)
    ret = models.Person.objects.filter(id__lt=5)   # less than
    ret = models.Person.objects.filter(id__gte=3)
    ret = models.Person.objects.filter(id__lte=3)
    ret = models.Person.objects.filter(id__gte=3,id__lte=5)

    ret = models.Person.objects.filter(id__in=[1,3,5,7,9,])     # in
    ret = models.Person.objects.exclude(id__in=[1,3,5,7,9,])    # not in

    ret = models.Person.objects.filter(name__contains=du)
    ret = models.Person.objects.filter(name__icontains=du)

    ret = models.Person.objects.filter(id__range=[1,5])
    ret = models.Person.objects.filter(name__startswith=)
    ret = models.Person.objects.filter(name__endswith=)

    # ret = models.Person.objects.filter(birth__contains=‘2018-06‘)
    # ret = models.Person.objects.filter(birth__month=6)
    # ret = models.Person.objects.filter(birth__day=1)

    print(ret)

外键

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")
    import django

    django.setup()

    from app01 import models

    # 正向查询
    # 基于对象查询

    # book_obj = models.Book.objects.filter(id=5).first()
    # print(book_obj.title)
    # print(book_obj.publisher)
    # print(book_obj.publisher.id)
    # print(book_obj.publisher.name)

    # 基于字段查询
    # ret = models.Book.objects.filter(publisher__name=‘沙河出版社‘)
    ret = models.Book.objects.filter(publisher__id=1)
    # print(ret)
    # for i in ret:
    #     print(i.publisher.name)

    # 反向查询
    # 没有指定related_name,使用pub_obj.book_set.all() 拿所有出版社关联的书籍对象
    # ret = models.Publisher.objects.get(id=1)
    # print(ret.book_set.all())

    # 指定related_name = ‘books‘,使用pub_obj.books.all() 拿所有出版社关联的书籍对象
    # print(ret.books.all())

    # 基于字段查询
    ret = models.Publisher.objects.filter(books__title=跟金老板学开车)
    print(ret)

多对多

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")
    import django

    django.setup()

    from app01 import models

    author_obj = models.Author.objects.first()
    # print(author_obj.books.all())
    # print(type(author_obj.books))

    # author_obj.books.create(title=‘跟金老板学美容美发‘, publisher_id=1)
    # 1. 先创建书籍对象
    # 2. 创建author_obj和新建书籍对象的对应关系

    # shahe = models.Publisher.objects.first()
    #
    # shahe.books.create(title=‘跟金老板学烫头3‘)
    # author_obj.books.set([])

    book_obj = models.Book.objects.first()
    # author_obj.books.clear()

    pub_obj = models.Publisher.objects.first()
    pub_obj.books.clear()

聚合和分组

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")
    import django

    django.setup()

    from app01 import models

    from django.db.models import Avg, Sum, Max, Min, Count

    # ret = models.Book.objects.aggregate(Sum(‘price‘),Count(‘id‘),pingjun=Avg(‘price‘),)
    # print(ret)

    # ret = models.Book.objects.annotate(Count(‘author‘),).values()
    # for i in ret:
    #     print(i)

    # ret = models.Publisher.objects.annotate(min_price=Min(‘books__price‘)).values()
    # for i in ret:
    #     print(i)

    # ret = models.Book.objects.annotate(author_num=Count(‘author‘)).filter(author_num__gt=1)
    # for i in ret:
    #     print(i)
    #
    # ret = models.Book.objects.annotate(author_num=Count("author")).order_by("-author_num")
    # print(ret)
    #

    ret = models.Author.objects.annotate(Sum(books__price)).values()
    print(ret)

F和Q

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")
    import django

    django.setup()

    from app01 import models
    # ret = models.Book.objects.filter(id__gt=1)
    # print(ret)

    from django.db.models import F, Q

    # ret = models.Book.objects.filter(kucun__lt=F(‘sale‘))
    # for i in ret:
    #     print(i.title,i.kucun,i.sale)

    # book_obj = models.Book.objects.get(id=1)
    # book_obj.title= ‘跟金老板学开潜艇‘
    # book_obj.save()

    # models.Book.objects.all().update(kucun=F(‘kucun‘)* 10)

    from django.db.models.functions import Concat
    from django.db.models import Value

    # models.Book.objects.all().update(title=F(‘title‘)+‘(第一版)‘)

    # models.Book.objects.all().update(title=Concat(F(‘title‘),Value(‘(第一版)‘)))

    ret = models.Book.objects.filter(Q(id__lt=3) | Q(id__gt=5))

    ret = models.Book.objects.filter(~Q(id=3), id__lt=5)

    print(ret)

事务

import os

if __name__ == __main__:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day68_orm.settings")
    import django
    django.setup()

    from app01 import models

    try:
        from django.db import transaction
        with transaction.atomic():
            models.Publisher.objects.create(name=沙河银行)
            models.Publisher.objects.get(id=100)

    except Exception as e:
        print(str(e))

 

orm

标签:rom   ESS   nsa   less   去重   没有   avg   str   字段排序   

原文地址:https://www.cnblogs.com/benson321/p/9484415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!