我正在使用ListView,我重写了get_queryset方法来进行搜索,并且我使用form来让用户输入值,这里出现问题:用户可以输入一些表单并将一些表单留空,所以表单.is_valid()方法总是返回false,我无法使用form.cleaned_data获取值,如何解决这个问题?
我的代码是这样的:
class GetStudentQueryHandler(ListView):
template_name = 'client.html'
paginate_by = STUDENT_PER_PAGE
context_object_name = 'studentinfo'
def get_context_data(self, **kwargs):
context = super(GetStudentQueryHandler, self).get_context_data(**kwargs)
context['can_show_distribute'] = self.request.user.has_perm('student.can_show_distribute_page')
context['queryform'] = QueryStudentForm
context['setstudentform'] = SetStudentForm
return context
def get_queryset(self):
form = QueryStudentForm(self.request.GET)
if form.is_valid():
form_data = form.cleaned_data
arguments = {}
for k, v in form_data.items():
if v:
arguments[k] = v
students = students.filter(**arguments)