目前,该网页显示按距离排序的找到结果列表(类似于ListView)。我想要做的是创建一个过滤器,以进一步缩小这些结果。
Views.py
def teacher_list(request, **kwargs):
if request.method == 'GET':
form = LocationForm(request.GET)
if form.is_valid():
SearchPoint = Point(form.cleaned_data['Lng'], form.cleaned_data['Lat'])
Radius = form.cleaned_data['SearchRadius']
Type = form.cleaned_data['Type']
else:
form = LocationForm()
SearchPoint = Point(0, 0)
Radius = form.cleaned_data['SearchRadius']
else:
form = LocationForm()
SearchPoint = Point(0, 0)
Radius = form.cleaned_data['SearchRadius']
results = Teacher.objects.filter(location__distance_lte=
(SearchPoint, D(km=Radius)))\
.annotate(distance=Distance('location', SearchPoint))\
.order_by('distance')
return render(request, "users/teacher_list.html", context={"form": form,"teacher_list":results,})
目前过滤有效,但不适用于分类变量。例如,如果我更改位置或搜索半径,表单会更新,我会看到新的结果。
但是,我有一个名为TYPE
的分类变量,它可以是FREE或PAID。如果某人选择了免费过滤器,则仅显示免费结果,反之亦然。这些是我模型中的布尔字段。
class Teacher(models.Model):
free = models.BooleanField()
paid = models.BooleanField()
我尝试添加的是某种类似于
的过滤器参数if Type == 'Free':
filter_variable = 'free=True'
elif Type == 'Paid':
filter_variable = 'paid=True'
else:
filter_variable =''
new_result_set = results.filter(filter_variable)
这是一种有效的过滤方式吗?我查看了django-filter但是看起来并没有与geodjango兼容,对我所需要的东西来说似乎太过分了。
答案 0 :(得分:1)
fs.writeFile('image.jpg',base64 string, {encoding: 'base64'}, function(err) {
console.log('File created');
});