I am using django for my application. I am new to it. I have the form where i multiple fields from where the user can perform search I have the relation hierarchy like Student has many subjects , then subjects has many assignments and assignements has many forms
我在应用程序中使用django。我是新手。我有一个表单,在这个表单中,我有多个字段,用户可以从其中进行搜索,我有关系层次结构,就像学生有很多科目,然后科目有很多作业,作业有很多形式
I have divide the forms in three sections where each section has 3-4 fields to search for e,g
我将表单分成三个部分,每个部分有3-4个字段来搜索e,g
STUDENT DATA
Name:
StudentID
city
SUBJECTS DATA
Start daTE
NAME
CREDITS
ASSIGNEMENT DATA
marks
date_issue
credits
I will be returning the list of students based on those seacrh criteria and they will be the partial matches.
我将根据seacrh标准返回学生列表,它们将是部分匹配。
I am new to django si i don't know how can i proceed with filtering of search because the search is also on nested objcets as well
我是django si的新手,我不知道如何进行搜索过滤,因为搜索也在嵌套的objcets中。
I can perform one column search like
我可以像这样执行一个列搜索
Student.objects.filter(sbjects__icontains="math")
but i don't know how can i simultaneously search for all
滤镜(sbjects__icontain ="math")但是我不知道如何同时搜索所有的东西
Can anyone give me the idea how to proceed
谁能告诉我该怎么做
3 个解决方案
#1
1
def your_view(request):
students = Student.objects.filter()
if request.GET.get('student'):
students = students.filter(
Q(name__contains=request.GET['student']) |
Q(studentID__contains=request.GET['student']) |
Q(city__contains=request.GET['student'])
)
if request.GET.get('subjects'):
students = students.filter(
Q(name__contains=request.GET['subjects']) |
Q(credits__contains=request.GET['subjects'])
)
//more search here
return render(request, 'page.html', {'students': students})
#2
1
So it seems that you are looking for OR
query? Something like that:
看来你是在找还是在查询?这样的:
from django.db.models import Q
Student.objects.filter( Q(subjects__icontains="math") | Q(name="John") )
The documentation:
文档:
https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
https://docs.djangoproject.com/en/dev/topics/db/queries/ complex-lookups-with-q-objects
#3
0
I suggest you take a loot at django-haystack plugin. It will allow you to make flattened indexes with model-like objects and then search very easy in them.
我建议你看看django-haystack插件。它将允许您使用类似模型的对象创建扁平索引,然后在其中搜索非常容易。
You can always go with Q objects and icontains lookups but it will be slow for relatively big amount of data.
您总是可以使用Q对象和icontain查找,但是对于相对较大的数据来说,查找速度会比较慢。
#1
1
def your_view(request):
students = Student.objects.filter()
if request.GET.get('student'):
students = students.filter(
Q(name__contains=request.GET['student']) |
Q(studentID__contains=request.GET['student']) |
Q(city__contains=request.GET['student'])
)
if request.GET.get('subjects'):
students = students.filter(
Q(name__contains=request.GET['subjects']) |
Q(credits__contains=request.GET['subjects'])
)
//more search here
return render(request, 'page.html', {'students': students})
#2
1
So it seems that you are looking for OR
query? Something like that:
看来你是在找还是在查询?这样的:
from django.db.models import Q
Student.objects.filter( Q(subjects__icontains="math") | Q(name="John") )
The documentation:
文档:
https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
https://docs.djangoproject.com/en/dev/topics/db/queries/ complex-lookups-with-q-objects
#3
0
I suggest you take a loot at django-haystack plugin. It will allow you to make flattened indexes with model-like objects and then search very easy in them.
我建议你看看django-haystack插件。它将允许您使用类似模型的对象创建扁平索引,然后在其中搜索非常容易。
You can always go with Q objects and icontains lookups but it will be slow for relatively big amount of data.
您总是可以使用Q对象和icontain查找,但是对于相对较大的数据来说,查找速度会比较慢。