在Django中过滤

时间:2011-03-04 04:37:16

标签: python django django-models django-templates django-views

我正在尝试在从下拉列表中选择的选项和文本字段之间实现一个简单的过滤方法。下拉列表中包含名称,名称,员工ID(DB中的所有字段)等选项。有一个文本字段,可以给出从指定中选择的选项的值。例如:如果我从下拉列表中选择“指定”,并在文本字段中选择“软件工程师”:我想显示名为Software Engineer的用户。如果我从下拉列表中选择名称并在文本字段中给出“John”,我想显示名为John的用户。我怎么能在Django做到这一点。我是Django的新手所以请帮助我这样做。我会在这里粘贴我的代码。 的 VIEWS.PY

def filter(request):
    val=request.POST.get('designation')      
    val2=request.POST.get('choices')
    val3=request.POST.get('textField')
    print val
    print val2,val3
    if val2=='Designation':       
        newData = EmployeeDetails.objects.filter(designation=request.POST.get('choices'))
        print val2
    elif val2=='Name':
        newData = EmployeeDetails.objects.filter(userName=request.POST.get('choices'))
        print val2
    elif val2=='EmployeeID':
        newData = EmployeeDetails.objects.filter(employeeID=request.POST.get('choices'))
        print val2    
    elif val2=='Project':
        newData = EmployeeDetails.objects.filter(project=request.POST.get('choices'))  
        print val2 
    elif val2=='DateOfJoin':
        newData = EmployeeDetails.objects.filter(dateOfJoin=request.POST.get('choices'))  
        print val2
    else:
        print "Data Not Found"       

    return render_to_response('filter.html',{'newData':newData,'val2':val2})      

HTML

<form action="http://10.1.0.90:8080/filter/" method="POST">
Filter By: 
<select name="choices" onsubmit="document.forms[0].submit()" >
    <option value="">Select A Choice</option>
    <option value="Name">Name</option> 
    <option value="Designation" >Designation</option>
    <option value="EmployeeID" >EmployeeID</option>
    <option value="Project" >Project</option>
    <option value="DateOfJoin" >Date Of Join</option>   
</select>
<input type="text" name="textField">
<input type="submit" value="Go">
</form>
<table>
{%for emp in emp_list.object_list%}
    <tr> <td><a href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td> </tr><td>
{%endfor%}
</table></h4>
{%for data in newData%}
{{ data.userName}}<br>
{%endfor%} 

MODELS.PY

class EmployeeDetails(models.Model):
    userName = models.CharField(max_length=200)
    designation = models.CharField(max_length=200)
    employeeID = models.IntegerField()
    contactNumber = models.CharField(max_length=200)
    project = models.CharField(max_length=200)
    dateOfJoin=models.TextField()

3 个答案:

答案 0 :(得分:4)

我在你的另一篇文章中给出了解决方案:

first, make your choice values EXACTLY the field names you want to search.

<select name="choices" onsubmit="document.forms[0].submit()" >
    <option value="">Select A Choice</option>
    <option value="userName">Name</option> 
    <option value="designation" >Designation</option>
    <option value="employeeID" >EmployeeID</option>
    <option value="project" >Project</option>
    <option value="dateOfJoin" >Date Of Join</option> 
</select>

然后,在您的视图中,使用关键字扩展将choices提供的字符串扩展为关键字参数。

if request.GET.get('choices'):
    # prevent query on field ''
    newdata= EmployeeDetails.objects.filter(**{request.GET.get('choices'): \
          request.GET.get('textField')})

    # this is the equivalent of running 
    #     EmployeeDetails.objects.filter(keyword=request.GET['textField'])

答案 1 :(得分:0)

def filter(request):
    val=request.POST.get('designation')      
    val2=request.POST.get('choices')
    val3=request.POST.get('textField')
    print val
    print val2,val3
    if val2=='Designation':

        newData = EmployeeDetails.objects.filter(designation=request.POST.get('textField'))
        print val2
    elif val2=='Name':
        newData = EmployeeDetails.objects.filter(userName=request.POST.get('textField'))
        print val2
    elif val2=='EmployeeID':
        newData = EmployeeDetails.objects.filter(employeeID=request.POST.get('textField'))
        print val2    
    elif val2=='Project':
        newData = EmployeeDetails.objects.filter(project=request.POST.get('textField'))  
        print val2 
    elif val2=='DateOfJoin':
        newData = EmployeeDetails.objects.filter(dateOfJoin=request.POST.get('textField'))  
        print val2
    else:
        print "Data Not Found"       

    return render_to_response('filter.html',{'newData':newData,'val2':val2})

这解决了我的问题

答案 2 :(得分:0)

为了补充Yuji的答案,我建议你阅读关于kwargs和Q表达式的内容。这些将帮助您进行此类动态查询。