Reputation: 2403
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()
Upvotes: 1
Views: 1943
Reputation: 118448
I gave the solution in your other post:
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>
Then, in your view, use keyword expansion to expand strings given by choices
into a keyword argument.
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'])
Upvotes: 4
Reputation: 101
To add to Yuji's answer, I suggest you to read about kwargs and Q expressions. These will help you make such dynamic queries.
Upvotes: 0
Reputation: 2403
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
Upvotes: 0