zoukankan      html  css  js  c++  java
  • Django之模型层,单,多表操作(二)

    一:单表操作之增,删,改

      1.1:增

        方式一:

          book_obj=Book.objects.create(title="python葵花宝典",state=True,price=100,publish="苹果出版社",pub_date="2012-12-12")

        方式二:

          book_obj=Book(title="python葵花宝典",state=True,price=100,publish="苹果出版社",pub_date="2012-12-12")

          book_obj.save()

      1.2:改

          Book.objects.filter(title__startswith="py").update(price=120)

          此外,update()方法对于任何结果集(QuerySet)均有效,这意味着你可以同时更新多条记录update()方法会返回一个整型数值,表示受影响的记录条数。

      1.3:删

      方式一:

        Entry.objects.filter(pub_date__year=2005).delete()

      方式二:

        b = Blog.objects.get(pk=1)

        # This will delete the Blog and all of its Entry objects.

        b.delete()

    二:单表查询

    from django.shortcuts import render,redirect,HttpResponse
    from app01.models import *
    # Create your views here.
    def    single_table(request):
            #创建表记录
            # student1 =student(name='djp',age=22,birthday='2018-10-10',flag=True,salary=48888.88)
            # student1.save()
            #  查询 与   QUERYset
            # 1查询全部
            stu=student.objects.all()  #  返回QuerySet[object,object2]
            print(stu)
    
    
            # 2:first ,last #返回object对象
            stu=student.objects.first() #返回object对象
            stu=student.objects.last()
    
            #3:filter 和get 和 exclude
            stu=student.objects.filter(name='yjp')  #返回Queryset[object1,object2]
            print(stu)
            stu = student.objects.exclude(name='yjp')  # 调用Queryset与条件不匹配 返回Queryset[object1,object2]
            print(stu)
            stu=student.objects.get(name='yjp')  #返回一个object 用get 有且只能查询到唯一的数据
            print(stu)
    
            #4:exists 和 count   调用均为 Queryset
            stu=student.objects.all().count()
            print(stu)
            stu=student.objects.all().exists()#判断查询数据是否存在,返回boolean
            stu=student.objects.filter(name='xxx').exists()
            print(stu)
    
    
            #5:order_by  和 reverse   排序  返回Queryset
            stu =student.objects.all().order_by('salary').reverse()
            print(stu)
    
    
            #6:values  和 values_list  调用者queryset
            stu =student.objects.all().order_by('salary').reverse().values('name')# 返回queryset
            print(stu)
            stu =student.objects.all().values('name','salary')#返回Queryset字典 [{},{}]也可以进行 first last 操作
            print(stu)#QuerySet [{'name': 'yjp', 'salary': Decimal('8888.88')}, {'name': '***', 'salary': Decimal('48888.88')}]
            stu=student.objects.all().values_list('name','salary')#返回Queryset [(),()]
            print(stu)#[('yjp', Decimal('8888.88')), ('***', Decimal('48888.88'))]>
    
            #7:distinct()去除重复记录  调用者quertset
            stu=student.objects.all().values('salary').distinct()
            print(stu)
    
            #8:基于双下划线的模糊查询
            # gt,lt   返回Queryset
            stu=student.objects.filter(salary__gt=9999)
            print(stu)
            # #注意   student.objects.==student.objects.all()
            stu =student.objects.all().filter(salary__lt=9999)
            print(stu)
    
            #in 存在 range 之间 contains 包含 icontains 不包含  year 年 startswith 以xx开头
            stu=student.objects.filter(salary__range=[1000,10000])
            print(stu)#返回Que ryset
            stu=student.objects.filter(salary__in=[1000,10000])
            print(stu)#返回Queryset
            stu=student.objects.filter(birthday__year='2018')
            print(stu)  #返回Queryset
    
    
    
    
            #9;update 和 delect 调用者Queryset  返回 被修改记录的条数
            # stu=student.objects.filter(name='djp').update(salary=7777.77)#返货
            # print(stu)
            # stu =student.objects.filter(name='djp').delete()
            # print(stu)
            t =Teacher.objects.all()
            print(t)
            return   HttpResponse('OK')
    View Code

    三:基于双下划线的模糊查询

    Book.objects.filter(price__in=[100,200,300])  # 存在
    Book.objects.filter(price__gt=100)            # 大于
    Book.objects.filter(price__lt=100)       # 小于 
    Book.objects.filter(price__range=[100,200])  # 之间
    Book.objects.filter(title__contains="python") # 是否是  返回boolean 
    Book.objects.filter(title__icontains="python")# 是否不是  返回boolean
    Book.objects.filter(title__startswith="py")   # 以xx开头
    Book.objects.filter(pub_date__year=2012)      # 日期的年是否是 2012
  • 相关阅读:
    CAN总线布线规范
    使用make_ext4fs时报错,No such file or directory
    安装arm-2009q3交叉编译器后,执行No such file....
    Busybox下make menconfig报错处理!
    解决Markdown转为PDF后,尖括号不能正确显示问题。
    开发板与PC直连 交叉、直连网线做法
    BusyBox tftp使用
    STM32的flash数据页转存过程分析!
    c语言中log函数的使用!
    POJ 3667 Hotel
  • 原文地址:https://www.cnblogs.com/yingjp/p/10088619.html
Copyright © 2011-2022 走看看