Django多字段搜索表单

时间:2014-08-04 21:42:45

标签: python django forms

我有一个查询表单,它从3个字段获取输入并显示数据库模型中的相关匹配内容。 但我试图从表单上的三个字段中只选择一个字段,并且需要从与该字段相关的数据库模型中获取数据?

views.py

from django.shortcuts import render
from search.forms import ModuleForm
from django.http import HttpResponse
from search.models import Module,Metamodule,Release

def searchview(request):
     if request.method == 'GET':
        form = ModuleForm(request.GET)  
        if form.is_valid():
           release_num = form.cleaned_data['release_num']
           metamodule_name = form.cleaned_data['metamodule_name']
           module_name = form.cleaned_data['module_name']
           results =    Module.objects.filter(metamodule__release__number=release_num).filter(metamodule__name=metamodule_name).filter(name=module_name)
           return render(request,'search/search_result.html',{'form': form, 'results': results})
     else:
          form = ModuleForm()    
          return render(request, 'search/search_form.html',{'form': form})

forms.py

from django import forms
from search.models import Module,Release,Metamodule

class ModuleForm(forms.Form):
     release_num = forms.ModelChoiceField(queryset=Release.objects.all(),empty_label='Pick a Release')
     metamodule_name = forms.ModelChoiceField(queryset=Metamodule.objects.all(),empty_label='Pick a Meta module')
     module_name = forms.ModelChoiceField(queryset=Module.objects.all(),empty_label='Pick a Module')

     def clean_release_number(self):
       try:
          release_num = self.cleaned_data.get["release_num"]
          metamodule_name = int(self.cleaned_data["metamodule_name"])
          module_name = int(self.cleaned_data["module_name"])
       except:
          release_num = None
          metamodule_name = None
          module_name = None

       if release_num and    Module.objects.exclude(metamodule__release__number=release_num).exists():
           raise forms.ValidationError("Please enter a valid release number.")
       else:
           return release_num          

如果修改视图以接受单个输入并显示数据,即使其他两个字段未提供数据?

1 个答案:

答案 0 :(得分:0)

考虑检查每个字段并单独过滤该字段值。

if form.is_valid():
    release_num = form.cleaned_data['release_num']
    metamodule_name = form.cleaned_data['metamodule_name']
    module_name = form.cleaned_data['module_name']
    results = Module.objects.all()
    if release_num:
        results = results.filter(metamodule__release__number=release_num)
    if metamodule_name:
        result = results.filter(metamodule__name=metamodule_name)
    if module_name:
        result = results.filter(name=module_name)
    return render(request,'search/search_result.html',{'form': form, 'results': results})

您可以使用clean_fieldname()方法验证输入。考虑使用def clean(self)执行多字段验证(例如至少填写一个字段)。

为什么要在GET处理表单输入?您是否有理由使用GET提交表单数据并忽略POST

此外,虽然您正在检查form.is_valid()并实施clean_fieldname方法,但您需要在else条件中添加if form.is_valid():条款。处理form.errors