FieldError at/. Cannot resolve keyword 'textField' into field

By : rv_k
Source: Stackoverflow.com
Question!

I am trying to do a filter method for my django application. I have some fields in db like, userName,designation,employeeID etc . Now i am trying to do a filtering; where there is drop down shows all the fields (name, designation, employeeID etc) and a text area for inputing some text. The filtering should be done based on the drop down choice selection and the data entered in the text area. While doing this i got this error: FieldError at /filter/ Cannot resolve keyword 'textField' into field. Choices are: contactNumber, dateOfJoin, designation, employeeID, id, project, userName I will paste my html and views.py here. Please help me to solve this.

<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="Date Of Join" >Date Of Join</option> 
</select>
<input type="text" name="textField">
<input type="submit" value="Go">
</form>

{%for data in newData%}
{{ data.userName}}<br>
{%endfor%} 

THIS is my VIEWS.PY

def filter(request):     
    val2=request.POST.get('choices')
    val3=request.POST.get('textField')
    print val2,val3
    newData = EmployeeDetails.objects.filter(choices=request.POST.get('choices'),textField=request.POST.get('textField'))
    return render_to_response('filter.html',{'newData':newData,'val2':val2,'val3':val3})    

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()

I am quite new to Django. Please help me to solve this

By : rv_k


Answers

If you don't have one field called "textField" you can't filter your "EmployeeDetails" models. You only can filter the "EmployeeDetails" models with available fields how: "contactNumber, dateOfJoin, designation, employeeID, id, project, userName".

=)

By : Rael Max


If i understand what you are trying to do, you are using filter the wrong way. To filter on a dynamic posted field(val2) with a given query(val3) try.

kwargs = {}
kwargs[val2] = val3

newData = EmployeeDetails.objects.filter(**kwargs)

the option values in you code would need to map the model for this to work.

By : JamesO


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