I have this model:
class Searches(models.Model):
user = models.ForeignKey(User)
.....other fields
Each user
has certain saved searches. I want to display a form
(a choiceSelect widget
) to select one of the several searches and click go. I want to filter
in the form
to display only the searches
of that particular User
. This is what I tried. but request.user
below does not work. how to fix this?
This is my form:
class SearchesForm(forms.Form):
#get the queryset for the searches model of logged in user
qset=Searches.objects.filter(user=request.user) ----> error
#get the most recent entry
default=Searches.objects.latest('id')
choices = forms.ModelChoiceField(queryset=qset,label='select search',initial=default)
EDIT: my view:
def display_search_user(request):
form=SearchesdiffForm(request)
if request.method=='POST':
search=SearchesdiffForm(request,data=request.POST)
print search
return HttpResponse(search)
return render(request,'search_list.html',{'form':form})