组合查询
一、两个外键条件筛选指定数据(两行筛选条件与最终结果数据是外键关系)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>组合查询简单版</title> 6 <style> 7 .border { 8 border: 1px solid red; 9 } 10 11 .btn a { 12 display: inline-block; 13 padding: 5px 8px; 14 border: 1px solid dimgrey; 15 text-decoration: none; 16 } 17 18 .top { 19 margin-top: 10px; 20 } 21 22 .left { 23 float: left; 24 } 25 26 .interval { 27 width: 120px; 28 height: 180px; 29 padding: 3px 5px; 30 } 31 32 .active { 33 background-color: orangered; 34 } 35 </style> 36 </head> 37 <body> 38 <div> 39 <h1>组合筛选</h1> 40 <div class="btn"> 41 {% if kwargs.classification_id == 0 %} 42 <a href="/video-0-{{ kwargs.level_id }}.html" class="active">全部</a> 43 {% else %} 44 <a href="/video-0-{{ kwargs.level_id }}.html">全部</a> 45 {% endif %} 46 {% for row in classification_list %} 47 {% if row.id == kwargs.classification_id %} 48 <a href="/video-{{ row.id }}-{{ kwargs.level_id }}.html" class="active">{{ row.name }}</a> 49 {% else %} 50 <a href="/video-{{ row.id }}-{{ kwargs.level_id }}.html">{{ row.name }}</a> 51 {% endif %} 52 {% endfor %} 53 </div> 54 <div class="btn top"> 55 {% if kwargs.level_id == 0 %} 56 <a href="/video-{{ kwargs.classification_id }}-0.html" class="active">全部</a> 57 {% else %} 58 <a href="/video-{{ kwargs.classification_id }}-0.html">全部</a> 59 {% endif %} 60 61 {% for row in level_list %} 62 {% if row.id == kwargs.level_id %} 63 <a href="/video-{{ kwargs.classification_id }}-{{ row.id }}.html" class="active">{{ row.name }}</a> 64 {% else %} 65 <a href="/video-{{ kwargs.classification_id }}-{{ row.id }}.html">{{ row.name }}</a> 66 {% endif %} 67 {% endfor %} 68 </div> 69 <div class="top"> 70 <h1>查询结果</h1> 71 {% for i in video_list %} 72 <div class="left border interval"> 73 <a href="{{ i.href }}"><img src="/{{ i.img }}" alt="" style="width: 120px;height: 140px;"></a> 74 <p>{{ i.name }}</p> 75 </div> 76 {% endfor %} 77 </div> 78 </div> 79 </body> 80 </html>
1 from django.shortcuts import render, HttpResponse, redirect 2 from app01.models import * 3 4 5 # Create your views here. 6 def video(request, *args, **kwargs): 7 # print(kwargs) # {'classification_id': '1', 'level_id': '3'} 8 condition = { 9 10 } 11 for k, v in kwargs.items(): 12 temp = int(v) 13 kwargs[k] = temp 14 if temp: 15 condition[k] = temp 16 17 print(condition) 18 classification_list = Classification.objects.all() 19 level_list = Level.objects.all() 20 video_list = Video.objects.all().filter(**condition) 21 print(video_list) 22 return render(request, "video.html", 23 {"classification_list": classification_list, "level_list": level_list, "video_list": video_list, 24 "kwargs": kwargs})
二、三个外键条件筛选指定数据(三行筛选条件与最终结果数据是外键关系)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>组合查询简单版</title> 6 <style> 7 .border { 8 border: 1px solid red; 9 } 10 11 .btn a { 12 display: inline-block; 13 padding: 5px 8px; 14 border: 1px solid dimgrey; 15 text-decoration: none; 16 } 17 18 .top { 19 margin-top: 10px; 20 } 21 22 .left { 23 float: left; 24 } 25 26 .interval { 27 width: 120px; 28 height: 180px; 29 padding: 3px 5px; 30 } 31 32 .active { 33 background-color: orangered; 34 } 35 </style> 36 </head> 37 <body> 38 <div> 39 <h1>组合筛选</h1> 40 <div class="btn"> 41 {% if kwargs.classification_id == 0 %} 42 <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="active">全部</a> 43 {% else %} 44 <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html">全部</a> 45 {% endif %} 46 {% for row in classification_list %} 47 {% if row.id == kwargs.classification_id %} 48 <a href="/video-{{ row.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html" 49 class="active">{{ row.name }}</a> 50 {% else %} 51 <a href="/video-{{ row.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html">{{ row.name }}</a> 52 {% endif %} 53 {% endfor %} 54 </div> 55 <div class="btn top"> 56 {% if kwargs.level_id == 0 %} 57 <a href="/video-{{ kwargs.classification_id }}-0-{{ kwargs.status }}.html" class="active">全部</a> 58 {% else %} 59 <a href="/video-{{ kwargs.classification_id }}-0-{{ kwargs.status }}.html">全部</a> 60 {% endif %} 61 62 {% for row in level_list %} 63 {% if row.id == kwargs.level_id %} 64 <a href="/video-{{ kwargs.classification_id }}-{{ row.id }}-{{ kwargs.status }}.html" 65 class="active">{{ row.name }}</a> 66 {% else %} 67 <a href="/video-{{ kwargs.classification_id }}-{{ row.id }}-{{ kwargs.status }}.html">{{ row.name }}</a> 68 {% endif %} 69 {% endfor %} 70 </div> 71 <div class="btn top"> 72 {% if kwargs.status == 0 %} 73 <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0.html" class="active">全部</a> 74 {% else %} 75 <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0.html">全部</a> 76 {% endif %} 77 78 {% for row in status_list %} 79 {% if row.id == kwargs.status %} 80 <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ row.id }}.html" class="active">{{ row.name }}</a> 81 {% else %} 82 <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ row.id }}.html">{{ row.name }}</a> 83 {% endif %} 84 {% endfor %} 85 </div> 86 <div class="top"> 87 <h1>查询结果</h1> 88 {% for i in video_list %} 89 <div class="left border interval"> 90 <a href="{{ i.href }}"><img src="/{{ i.img }}" alt="" style="width: 120px;height: 140px;"></a> 91 <p>{{ i.name }}</p> 92 </div> 93 {% endfor %} 94 </div> 95 </div> 96 </body> 97 </html>
1 from django.shortcuts import render, HttpResponse, redirect 2 from app01.models import * 3 4 5 # Create your views here. 6 7 8 def video1(request, *args, **kwargs): 9 condition = {} 10 for k, v in kwargs.items(): 11 temp = int(v) 12 kwargs[k] = temp 13 if temp: 14 condition[k] = temp 15 16 classification_list = Classification.objects.all() 17 level_list = Level.objects.all() 18 status_list = list(map(lambda x: {"id": x[0], "name": x[1]}, Video.status_choice)) 19 video_list = Video.objects.all().filter(**condition) 20 return render(request, "video1.html", 21 {"classification_list": classification_list, "level_list": level_list, "video_list": video_list, 22 "kwargs": kwargs, "status_list": status_list})
三、第一个条件和第二个条件是manytomany关系,第二个条件和第三个条件都是结果数据的外键。(也就是第一行筛选条件和最终结果无直接关系)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>组合查询简单版</title> 6 <style> 7 .border { 8 border: 1px solid red; 9 } 10 11 .btn a { 12 display: inline-block; 13 padding: 5px 8px; 14 border: 1px solid dimgrey; 15 text-decoration: none; 16 } 17 18 .top { 19 margin-top: 10px; 20 } 21 22 .left { 23 float: left; 24 } 25 26 .interval { 27 width: 120px; 28 height: 180px; 29 padding: 3px 5px; 30 } 31 32 .active { 33 background-color: orangered; 34 } 35 </style> 36 </head> 37 <body> 38 <div> 39 <h1>组合筛选</h1> 40 <div class="btn"> 41 {% if kwargs.direction_id == 0 %} 42 <a href="/video2-0-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html" class="active">全部</a> 43 {% else %} 44 <a href="/video2-0-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html">全部</a> 45 {% endif %} 46 {% for i in direction_list %} 47 {% if kwargs.direction_id == i.id %} 48 <a href="/video2-{{ i.id }}-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html" 49 class="active">{{ i.name }}</a> 50 {% else %} 51 <a href="/video2-{{ i.id }}-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html">{{ i.name }}</a> 52 {% endif %} 53 {% endfor %} 54 </div> 55 <div class="btn top"> 56 {% if kwargs.classification_id == 0 %} 57 <a href="/video2-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html" class="active">全部</a> 58 {% else %} 59 <a href="/video2-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html">全部</a> 60 {% endif %} 61 {% for i in classification_list %} 62 {% if kwargs.classification_id == i.id %} 63 <a href="/video2-{{ kwargs.direction_id }}-{{ i.id }}-{{ kwargs.level_id }}.html" 64 class="active">{{ i.name }}</a> 65 {% else %} 66 <a href="/video2-{{ kwargs.direction_id }}-{{ i.id }}-{{ kwargs.level_id }}.html">{{ i.name }}</a> 67 {% endif %} 68 {% endfor %} 69 </div> 70 <div class="btn top"> 71 {% if kwargs.level_id == 0 %} 72 <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html" class="active">全部</a> 73 {% else %} 74 <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html">全部</a> 75 {% endif %} 76 {% for i in level_list %} 77 {% if kwargs.level_id == i.id %} 78 <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ i.id }}.html" 79 class="active">{{ i.name }}</a> 80 {% else %} 81 <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ i.id }}.html">{{ i.name }}</a> 82 {% endif %} 83 {% endfor %} 84 </div> 85 86 <hr> 87 <div class="top"> 88 <h1>查询结果</h1> 89 {% for i in video_list %} 90 <div class="left border interval"> 91 <a href="{{ i.href }}"><img src="/{{ i.img }}" alt="" style="width: 120px;height: 140px;"></a> 92 <p>{{ i.name }}</p> 93 </div> 94 {% endfor %} 95 </div> 96 </div> 97 </body> 98 </html>
1 from django.shortcuts import render, HttpResponse, redirect 2 from app01.models import * 3 4 5 # Create your views here. 6 def video(request, *args, **kwargs): 7 # print(kwargs) # {'classification_id': '1', 'level_id': '3'} 8 condition = { 9 10 } 11 for k, v in kwargs.items(): 12 temp = int(v) 13 kwargs[k] = temp 14 if temp: 15 condition[k] = temp 16 17 print(condition) 18 classification_list = Classification.objects.all() 19 level_list = Level.objects.all() 20 video_list = Video.objects.all().filter(**condition) 21 print(video_list) 22 return render(request, "video.html", 23 {"classification_list": classification_list, "level_list": level_list, "video_list": video_list, 24 "kwargs": kwargs}) 25 26 27 def video1(request, *args, **kwargs): 28 condition = {} 29 for k, v in kwargs.items(): 30 temp = int(v) 31 kwargs[k] = temp 32 if temp: 33 condition[k] = temp 34 35 classification_list = Classification.objects.all() 36 level_list = Level.objects.all() 37 status_list = list(map(lambda x: {"id": x[0], "name": x[1]}, Video.status_choice)) 38 video_list = Video.objects.all().filter(**condition) 39 return render(request, "video1.html", 40 {"classification_list": classification_list, "level_list": level_list, "video_list": video_list, 41 "kwargs": kwargs, "status_list": status_list}) 42 43 44 def video2(request, *args, **kwargs): 45 # 问题解决思路 46 # if direction_id==0 47 # if classification_id==0 48 # pass 49 # else: 50 # condition["classification_id"]=classification_id 51 # 52 # else direction_id!=0 53 # if classification_id==0 54 # condition["classification_id__in"]=[该direction下的所有classification的id列表] 55 # else: 56 # if classification_id in [该direction下的所有classification的id列表]: 57 # condition["classification_id"]=classification_id 58 # else : 59 # condition["classification_id__in"]=[该direction下的所有classification的id列表] 60 condition = {} 61 for k, v in kwargs.items(): 62 kwargs[k] = int(v) 63 direction_id = kwargs["direction_id"] 64 classification_id = kwargs["classification_id"] 65 level_id = kwargs["level_id"] 66 67 direction_list = Direction.objects.all() 68 if direction_id == 0: 69 classification_list = Classification.objects.all() 70 if classification_id == 0: 71 pass 72 else: 73 condition["classification_id"] = classification_id 74 else: 75 classification_list = Direction.objects.filter(id=direction_id).first().classification.all() 76 cls_list = list(zip(*classification_list.values_list("id")))[0] 77 if classification_id == 0: 78 condition["classification_id__in"] = cls_list 79 else: 80 if classification_id in cls_list: 81 condition["classification_id"] = classification_id 82 else: 83 kwargs["classification_id"] = 0 # 点睛之笔,如果不存在该分类重置 全部 84 condition["classification_id__in"] = cls_list 85 if level_id: 86 condition["level_id"] = level_id 87 else: 88 pass 89 level_list = Level.objects.all() 90 91 video_list = Video.objects.filter(**condition) 92 return render(request, "video2.html", {"direction_list": direction_list, "classification_list": classification_list, 93 "level_list": level_list, "video_list": video_list, "kwargs": kwargs})