from django.shortcuts import render, HttpResponse, redirect, render_to_response import datetime from django.template import RequestContext, Template from django.db.models import Avg, Min, Max, Sum, F, Q, QuerySet from task import models # Create your views here. # def index(req): # class Person: # def __init__(self, name, age): # self.name = name # self.age = age # # s = "hello" # # 列表 # s1 = [1, 22, 333] # # 字典 # s3 = {"username": "alex", "sex": "jjj"} # s4 = datetime.datetime.now() # p = Person("yuan", 18) # return render(req, "index.html", {"obj": s}) # # # def login(req): # num = 12 # if req.method == "POST": # return HttpResponse("ok") # return render(req, "login.html", locals()) # # 注意这里出现的问题 # # return render_to_response("login.html", locals(), content_type=RequestContext(req)) # # # L1 = [1, 2, 3, 4, 5, 6] # # L2 = [] # # """ # is ,is not ,in, not in ,==等等关键字的判断 # """ # # # # 添加对象 # def data_ope(req): # # 添加对象 # # pub = models.Publish.objects.filter(id=1) # Book.objects.create( # title="漂流记", # price=1, # color="yellow", # ) # # 两种传参的方法,保存的时候通过save的方法去执行 # Book.objects.create(**{ # "title": "漂流记", # "price": 1, # "color": "yellow" # }) # pass # # # # 删除 # def det(req): # # filter是查找值 # Book.objects.filter(id=1).delete() # # # # 修改 # def update(req): # # 修改1 # aut = Book.objects.get(id=5) # aut.title = "ddd" # aut.save() # 所有的当前行都会修改,所以效率特别低 # # 修改二,QuerySet对象调用update()方法,而get方法获取的结果是不能调用update()方法的 # book = Book.objects.filter(id=2).update(title="eeee") # # # # 查找 # def find(req): # """ # list :filter 获取的是一个QuerySet的集合对象是对应的key_value值 # list :all()获取所有的结果 # get()获取特定的某个结果 # 对查询的结果进行处理 # 惰性机制: # 在以上三个方法中进行的时候,并没有真正意义上的查询sql,查询结果 # 集对象,而是当调用querySet的时候才会去执行sql语句 # :param req: # :return: # """ # book = Book.objects.filter(id=2).values("price") # # # # 多对多向其中添加数据 # def Multi(req): # # 正向查找 # author = models.Author.objects.filter(id=2)[0] # 获取的是一个QuerySet # book = models.Book.objects.filter(id=1)[0] # book.author.add(*author) # *代表的是一个序列 # book.author.remove(*author) # # 反向查找 # author.book_set.add(*book) # *代表的是一个序列 # author.book_set.remove(*book) # pass # # # def demo(): # if 1 is not 2: # 前面是后面的一个对象,或者可以理解为子集的关系 # L2.append(1) # print(L2) # # # # QuerySet什么时候执行 # # 通过打印日志的方法去验证,这样的话就能验证他的惰性机制的处理 # # # 订单 # def ordered(req): # return render(req, "ordered.html") # # # # 购物车 # def shopping_car(req): # return render(req, "shopping_car.html") # # # # if __name__ == '__main__': # # demo() # 所谓的many_to_many其实就是两张foreginKey建立两张表 def insert(req): models.Book.objects.create( publisher_id=1, title="hello", authors="黎明", price=1, color="red", publication_date="2019-11-02", ) def add_obj(req): # 不会触发sql语句querySet,需要的时候才会sql set = models.Book.objects.filter(id=2) for i in set: print(i) return HttpResponse("ok") # 正向查找 def find_Go(req): # 查找出对象,从而根据对象查找id,在根据id查找对象显示名称 # 正向查找 bookObj = models.Book.objects.filter(title="hello") # 第一种for循环判断 # for name in bookObj: # city = name.publisher.city # var = name.publisher.name # return HttpResponse(city + var) city = bookObj.values("title").distinct() # values()遍历的结果获取的是一个一个的对象 city1 = bookObj.iterator() print(city1) # city = bookObj.publisher.city return HttpResponse(city) # 不用写sql语句 # 反向查找 def reverse_find(req): # 查找出版社 obj = models.Publisher.objects.filter(name="fdfs")[0] return HttpResponse(obj.book_set.all().values("title").distinct().count()) pass # 双下划线查询--单表操作(条件) def conditon(req): # 大于 models.Book.objects.filter(id__gt=2, id__lt=5) # 两个条件都满足就可以执行 # 小于 models.Book.objects.filter(id__lt=2) # models.Book.objects.filter(id__in=[2, 3]) models.Book.objects.filter(title__contains="sf") # 不区分大写---还有好多 models.Book.objects.filter(title__icontains="sf") models.Book.objects.filter(id__range=[1, 2]) # between --and --区范围 models.Book.objects.filter(title__istartswith="P") # pass # 多条件查询--双下划线 def MutiCondition(req): # 关联表查询 result = models.Publisher.objects.filter(book__title="ss", book__id__gt=5).values("name") print(result.query) # 可以打印出sql语句 # ""中相当于前端传的数据,是列名,两个列名中都是可以使用的,但是能不能同时使用呢? models.Book.objects.filter(title="ddd").values("publisher__name") print(result) pass # 聚合查询 """ aggregate本身是聚合函数,而聚合方法必须放到聚合函数中去, 是一个建和值的函数,而键是根据方法的名称和变量值自动生成的,当然也可以进行自己定义 """ def aggregatefunc(req): models.Book.objects.all().aggregate(avrPrice=Avg('price')) pass # 分组查询 def func(req): models.Book.objects.values("author__name").annotate(Sum("price")) pass # F查询,就是对列的数值进行操作 def F_Query(req): models.Book.objects.all().update(price=F("price") + 20) pass # Q查询 |表示或者的关系 def Q_query(req): result = models.Book.objects.filter(Q(price__gt=50) & (Q(id=3) | Q(title="php")), color="ffff") # 如果是地址值,则看Book中有没有__str__方法 print(result) pass