组合搜索速查_多对多
2018-03-03 本文已影响15人
两点半的杂货铺
一、分析
image.png上面就一个一个多对多关系,首先先弄明白一个问题,那就是所有的数据展示,都是最先通过url出现的 ,所以url这里的第一传入值
接着要思考几种情况
-
1.当直接进入时一个选项直接点击的全部,也就是查询all
注这里要注意的是第二个参数,第一个参数在大条件下已经默认0查询全部
+①所以第一种可能展示所有课程http://127.0.0.1:8000/video1/0-0-0
②第二种可能就是连接选择其中一个选项http://127.0.0.1:8000/video1/0-1-0
-
2.如果最后这时候用户在大条件下输入了一个查询条件,也就是第一个参数不为零
http://127.0.0.1:8000/video1/1-0-0
①所以第一种可能展示所有课程http://127.0.0.1:8000/video1/1-0-0
这里要展示上面数据关联下面数据,多对多的所有展示
②所以第二种可能展示所有课程http://127.0.0.1:8000/video1/1-1-0
用户输入的在我们的多对多关系中
③用户输入的不在我们的关系中http://127.0.0.1:8000/video1/1-10000-0
二、代码思路
+数据库
from django.db import models
# Create your models here.
class Work(models.Model):
name = models.CharField(max_length=32)
Language = models.ManyToManyField('Language')
class Meta:
db_table = 'Work'
verbose_name_plural = "工作"
def __str__(self):
return self.name
class Language(models.Model):
name = models.CharField(max_length=32)
class Meta:
db_table = 'Language'
verbose_name_plural = "语言"
def __str__(self):
return self.name
class Level(models.Model):
title = models.CharField(max_length=32)
class Meta:
verbose_name_plural = '难度级别'
def __str__(self):
return self.title
class Video(models.Model):
title = models.CharField(max_length=32)
abstract = models.CharField(max_length=32)
language = models.ForeignKey(Language)
level = models.ForeignKey(Level)
class Meta:
db_table = 'Video'
verbose_name_plural = '视频'
def __str__(self):
return self.title
- url 编写
这里的url对应参数。最好用数据库对应字段来展示对应
url(r'video1/
(?P<work_id>\d+)-
(?P<language_id>\d+)-
(?P<level_id>\d+)',views.query_video1),
- views层
这里要注意几点,第一点就是我们把每次符合需求的结果封装成字典方便,查找第二次点就是要提前吧url中的str转换成int
①这里对应上面第一点
def query_video1(request,**kwargs):
concent ={}
for k,v in kwargs.items():
temp = int(v)
kwargs[k] =temp
work_list = models.Work.objects.all()
work_id = kwargs.get("work_id")
language_id = kwargs.get("language_id")
level_id = kwargs.get("level_id")
if work_id == 0:
langage_list = models.Language.objects.all()
if language_id:
concent['language_id'] = language_id
②这里对应上面第二点,主要注意多对多的点
else:
#多对多 想去找 那个工作,找到这个工作后,去联合表找,对应的语言
Work_obj = models.Work.objects.filter(id = work_id).first()
if Work_obj:
langage_list = Work_obj.Language.all()
v = Work_obj.Language.values_list("id")
if v :
langage_list_id = list(zip(*v))[0]
else:
langage_list_id = []
if language_id == 0:
concent['language_id__in']=langage_list_id
else:
if language_id in langage_list_id:
concent['language_id']=language_id
else:
print(111111111111111111111111)
kwargs['language_id']=0
concent['language_id__in'] = langage_list_id
else:
kwargs['language_id'] = 0
kwargs['work_id'] = 0
langage_list = models.Language.objects.all()
if level_id ==0:
pass
else:
concent['level_id'] = level_id
level_list = models.Level.objects.all()
video = models.Video.objects.filter(**concent)
print(video)
return render(request,
'index1.html',
{ "work_list":work_list,
"kwargs": kwargs,
"langage_list": langage_list,
"level_list": level_list,
"video": video,
}
)
- html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.concent a{
display: inline-block;
padding: 3px 5px;
border:1px solid #dddddd;
margin: 5px 5px;
}
.concent a.active{
background-color:yellowgreen;
}
</style>
</head>
<body>
<div class="concent">
<div>
{% if kwargs.work_id == 0 %}
<a class="active" href="/video1/0-{{kwargs.language_id}}-{{kwargs.level_id}}">全部</a>
{% else %}
<a href="/video1/0-{{kwargs.language_id}}-{{kwargs.level_id}}">全部</a>
{% endif %}
{% for i in work_list %}
{% if kwargs.work_id == i.id %}
<a class="active" href="/video1/{{i.id}}-{{kwargs.language_id}}-{{kwargs.level_id}}" >{{ i.name }}</a>
{% else%}
<a href="/video1/{{i.id}}-{{kwargs.language_id}}-{{kwargs.level_id}}" >{{ i.name }}</a>
{% endif %}
{% endfor %}
</div>
<div>
{% if kwargs.language_id == 0 %}
<a class="active" href="/video1/{{kwargs.work_id}}-0-{{kwargs.level_id}}">全部</a>
{% else %}
<a href="/video1/{{kwargs.work_id}}-0-{{kwargs.level_id}}">全部</a>
{% endif %}
{% for i in langage_list %}
{% if kwargs.language_id == i.id %}
<a class="active" href="/video1/{{kwargs.work_id}}-{{i.id}}-{{kwargs.level_id}}" >{{ i.name }}</a>
{% else%}
<a href="/video1/{{kwargs.work_id}}-{{i.id}}-{{kwargs.level_id}}" >{{ i.name }}</a>
{% endif %}
{% endfor %}
</div>
<div>
{% if kwargs.level_id == 0 %}
<a class="active" href="/video1/{{kwargs.work_id}}-{{kwargs.language_id}}-0">全部</a>
{% else %}
<a href="/video1/{{kwargs.work_id}}-{{kwargs.language_id}}-0">全部</a>
{% endif %}
{% for i in level_list %}
{% if kwargs.level_id == i.id %}
<a class="active" href="/video1/{{kwargs.work_id}}-{{kwargs.language_id}}-{{i.id}}">{{ i.title }}</a>
{% else%}
<a href="/video1/{{kwargs.work_id}}-{{kwargs.language_id}}-{{i.id}}">{{ i.title }}</a>
{% endif %}
{% endfor %}
</div>
</div>
<div>
<h1>展示</h1>
{% for i in video %}
{{ i.title }}
{% endfor %}
</div>
</body>
</html>