Filtering in Django

By : rv_k
Source: Stackoverflow.com
Question!

I am trying to implement a simple filter method between a choice selected from drop-down and a text field. The drop down has choices like Name, Designation,Employee ID (which are all fields in DB). There is a text field where a value for the selected choice from designation can be given. Eg: If i select Designation from drop down, and 'Software Engineer' in text field: i want to display the users with designation Software Engineer. If i select Name from drop-down and given "John" in text field, i want to display users with name John. How can i do this in Django. I am quite new to Django so please help me to do this. I will paste my code here. 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()
By : rv_k


Answers
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})

This fixed my problem

By : rv_k


To add to Yuji's answer, I suggest you to read about kwargs and Q expressions. These will help you make such dynamic queries.

By : Hari


This video can help you solving your question :)
By: admin