如何在搜索栏中按布尔字段进行过滤 - django
How to filter by boolean field in search bar - django
我正在开发一个 Django 项目,它是一个项目管理系统。
基本上在导航栏中,我实现了一个搜索器来搜索特定项目,如标题、客户、描述等。
我想给他们搜索紧急项目的可能性(在我的项目模型中紧急是一个布尔字段),但我实现它的方式不起作用。
这是我的项目模型:
class Project(models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=1000)
client = models.ForeignKey(Cliente, on_delete=models.CASCADE)
complete = models.BooleanField(default=False)
urgent = models.BooleanField(default=False)
deadline = models.DateField()
...
制作搜索栏(在我的 html 文件中是:)
<form method="get" class="form-inline">
<input class="form-control mr-sm-2" name="q" id="id_q" type="text" placeholder="Search..." aria-label="Search" value="{{query}}">
<button class="btn btn-outline-dark my-2 my-sm-0" type="submit">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-search" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M10.442 10.442a1 1 0 0 1 1.415 0l3.85 3.85a1 1 0 0 1-1.414 1.415l-3.85-3.85a1 1 0 0 1 0-1.415z"/>
<path fill-rule="evenodd"
d="M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM13 6.5a6.5 6.5 0 1 1-13 0 6.5 6.5 0 0 1 13 0z"/>
</svg>
</button>
</form>
我是如何实现这个功能的:
def get_projects_qs(proj, query=None):
queryset = []
queries = query.split(" ")
for q in queries:
projects = proj.filter( Q(title__icontains=q) | Q(description__icontains=q) | Q(urgent__icontains=q) | Q(client__client__icontains=q) ).distinct()
for project in projects:
queryset.append(project)
return list(set(queryset))
现在,如果我执行 Q(urgent_icontains=q) 它不起作用,但是如果我执行 Q(urgent=q) 它只在第一次工作然后我得到这个错误:
ValidationError at /marketing/
['“” value must be either True or False.']
假设我们有这样一个模型:
class Project(models.Model):
title = models.CharField(max_length=255)
complete = models.BooleanField(default=False)
urgent = models.BooleanField(default=False)
.
.
.
views.py:
from . models import Product
def is_valid_queryparam(param):
return param != '' and param is not None
def search(request):
queryset = Product.objects.all()
search = request.GET.get('search')
complete = request.GET.get('complete')
urgent = request.GET.get('urgent')
if is_valid_queryparam(search):
queryset = queryset.filter(title__icontains=search)
if is_valid_queryparam(complete):
queryset = queryset.filter(complete=True)
elif is_valid_queryparam(urgent):
queryset = queryset.filter(urgent=True)
context = {
'queryset': queryset
}
return render(request, 'search.html', context)
现在在模板中(search.html):
<form id="mySearch" method="GET">
<input type="search" name="search" placeholder="Search..." />
<button" type="submit">Click to Search</button>
<input type="checkbox" name="complete" id="checkbox-1" onchange="$('#mySearch').submit();" {% if request.GET.complete == 'on' %} checked {% endif %}>
<label for="checkbox-1">complete</label>
<input type="checkbox" name="urgent" id="checkbox-2" onchange="$('#mySearch').submit();" {% if request.GET.urgent == 'on' %} checked {% endif %}>
<label for="checkbox-2">urgent</label>
</form>
我正在开发一个 Django 项目,它是一个项目管理系统。 基本上在导航栏中,我实现了一个搜索器来搜索特定项目,如标题、客户、描述等。
我想给他们搜索紧急项目的可能性(在我的项目模型中紧急是一个布尔字段),但我实现它的方式不起作用。
这是我的项目模型:
class Project(models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=1000)
client = models.ForeignKey(Cliente, on_delete=models.CASCADE)
complete = models.BooleanField(default=False)
urgent = models.BooleanField(default=False)
deadline = models.DateField()
...
制作搜索栏(在我的 html 文件中是:)
<form method="get" class="form-inline">
<input class="form-control mr-sm-2" name="q" id="id_q" type="text" placeholder="Search..." aria-label="Search" value="{{query}}">
<button class="btn btn-outline-dark my-2 my-sm-0" type="submit">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-search" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M10.442 10.442a1 1 0 0 1 1.415 0l3.85 3.85a1 1 0 0 1-1.414 1.415l-3.85-3.85a1 1 0 0 1 0-1.415z"/>
<path fill-rule="evenodd"
d="M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM13 6.5a6.5 6.5 0 1 1-13 0 6.5 6.5 0 0 1 13 0z"/>
</svg>
</button>
</form>
我是如何实现这个功能的:
def get_projects_qs(proj, query=None):
queryset = []
queries = query.split(" ")
for q in queries:
projects = proj.filter( Q(title__icontains=q) | Q(description__icontains=q) | Q(urgent__icontains=q) | Q(client__client__icontains=q) ).distinct()
for project in projects:
queryset.append(project)
return list(set(queryset))
现在,如果我执行 Q(urgent_icontains=q) 它不起作用,但是如果我执行 Q(urgent=q) 它只在第一次工作然后我得到这个错误:
ValidationError at /marketing/
['“” value must be either True or False.']
假设我们有这样一个模型:
class Project(models.Model):
title = models.CharField(max_length=255)
complete = models.BooleanField(default=False)
urgent = models.BooleanField(default=False)
.
.
.
views.py:
from . models import Product
def is_valid_queryparam(param):
return param != '' and param is not None
def search(request):
queryset = Product.objects.all()
search = request.GET.get('search')
complete = request.GET.get('complete')
urgent = request.GET.get('urgent')
if is_valid_queryparam(search):
queryset = queryset.filter(title__icontains=search)
if is_valid_queryparam(complete):
queryset = queryset.filter(complete=True)
elif is_valid_queryparam(urgent):
queryset = queryset.filter(urgent=True)
context = {
'queryset': queryset
}
return render(request, 'search.html', context)
现在在模板中(search.html):
<form id="mySearch" method="GET">
<input type="search" name="search" placeholder="Search..." />
<button" type="submit">Click to Search</button>
<input type="checkbox" name="complete" id="checkbox-1" onchange="$('#mySearch').submit();" {% if request.GET.complete == 'on' %} checked {% endif %}>
<label for="checkbox-1">complete</label>
<input type="checkbox" name="urgent" id="checkbox-2" onchange="$('#mySearch').submit();" {% if request.GET.urgent == 'on' %} checked {% endif %}>
<label for="checkbox-2">urgent</label>
</form>