Job Search Engine
876 浏览 5 years, 8 months
2.18 列表视图提升 (2) 时间段搜索和统计
版权声明: 转载请注明出处 http://www.codingsoho.com/列表视图提升 (2) 时间段搜索和统计
添加搜索字段
添加搜索字段 start和end
class JobEntryFilter(FilterSet):
q = CharFilter(method="q_filter", distinct=True)
title = CharFilter(name='title', lookup_expr='icontains', distinct=True)
region = CharFilter(name='region', lookup_expr='icontains', distinct=True)
start = CharFilter(name='created', lookup_expr='gte', distinct=True)
end = CharFilter(name='created', lookup_expr='lte', distinct=True)
class Meta:
model = JobEntry
fields = [
'title',
'region',
'start',
'end',
]
修改表单
添加attr,便于模板访问
class JobEntryFilterForm(forms.Form):
q = forms.CharField(label=_('Search'), required=False)
title = forms.CharField(label=_('title'), required=False)
region = forms.CharField(label=_('region'), required=False)
start = forms.DateField(label=_("start"), required=False)
end = forms.DateField(label=_("end"), required=False)
def __init__(self, *args, **kwargs):
super(JobEntryFilterForm, self).__init__(*args, **kwargs)
self.fields['start'].widget.attrs['class'] ="calenda"
self.fields['end'].widget.attrs['class'] ="calenda"
datepick
<style>
{% block style %}
.form-inline .form-group:nth-child(3) input,
.form-inline .form-group:nth-child(4) input,
.form-inline .form-group:nth-child(5) input{
max-width: 100px;
}
{% endblock %}
</style>
<script>
{% block jquery %}
$('input.calenda').datepicker({
dateFormat: "yy-mm-dd", //$(this).val() can define format in widget numberOfMonths: 2,
changeYear: true,
changeMonth: true,
showButtonPanel: true,
showWeek: true,
});
$(function () {
$("[data-toggle='popover']").popover();
});
{% endblock jquery%}
</script>
这个库有jquery-ui提供
<link rel="stylesheet" href="[https://cdn.bootcss.com/jqueryui/1.11.0/jquery-ui.min.css](https://cdn.bootcss.com/jqueryui/1.11.0/jquery-ui.min.css)">
<script src="[https://cdn.bootcss.com/jqueryui/1.10.2/jquery-ui.min.js](https://cdn.bootcss.com/jqueryui/1.10.2/jquery-ui.min.js)"></script>
数据库 2019-02-07 14:44:53
如果时间选择 开始 和结束都是 2019-02-07,那么结果为空,因为数据库为DateTime,而比较值用的Date,它自动转DateTime时应该转的 2019-02-07 00:00:00,所以应该将结束时间+1,设为第二天的0点
class JobEntryFilter(FilterSet):
...
end = CharFilter(method="end_filter", distinct=True)
def end_filter(self, queryset, field, value):
value += ' 23:59:59'
qs = queryset.filter(Q(created__lte=value)).distinct()
return qs