Am working on this app where users of the app can make payment to other users, but am currently challenge by the search functionality, since i can only search users on the app by their foreignKey only, but instead i would want to be able to search by users full name, email and username, when i search user by name i get this exception (Field 'id' expected a number but got 'Hong'. ). why does my query only limit me to search by user number(foreignKey).Here is my view to search user for payment.
class RequestSearchUser(LoginRequiredMixin, ListView):
model = FriendList
form_class = SearchUserForm
template_name = 'wallet/request_search_user_form.html'
def get_queryset(self):
try:
username = self.kwargs['search_user']
except:
username = ''
if username != '':
object_list = self.model.objects.filter(username=username)
else:
object_list = self.model.objects.all()
return object_list
def get_context_data(self, **kwargs):
context = super(RequestSearchUser, self).get_context_data(**kwargs)
query = self.request.GET.get("search_user")
context['user'] = self.request.user
if query:
queryset = (Q(user=query))
search_user = FriendList.objects.filter(queryset)
if not search_user:
messages.error(self.request, 'No user found.')
else:
search_user = []
context['search_user'] = search_user
context['nbar'] = 'request'
return context
Here is my search form.py
class SearchUserForm(forms.ModelForm):
class Meta:
model = User
fields = ('username',)
def clean_username(self):
username = self.cleaned_data['username'].strip()
return username
Here is the Html form
<div >
<h2>Request Money</h2><hr>
<form action="{% url 'wallet:request' %}" method="get" novalidate="novalidate">
<div >
<div >
{% if messages %}
{% for message in messages %}
<div role="alert">
{{ message }}
</div>
{% endfor %}
{% endif %}
</div>
<div >
{% for search_user in search_user %}
{% if search_user == request.user %}
<div role="alert">
Cannot input your username.
</div>
{% else %}
<meta http-equiv="refresh" content="0; url={% url 'wallet:request_money' search_user.id %}" />
{% endif %}
{% endfor %}
</div>
<div >
<div >
<input name="search_user" id="search_user" type="text" maxlength="45"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'Username'" placeholder="Username" required/>
</div>
</div>
</div>
<div >
<button type="submit" >Search </button>
</div>
</form>
</div>
</article>
CodePudding user response:
Error is here
object_list = self.model.objects.filter(user=username)
and here
queryset = (Q(user=query))
If you store username of user in field called username, you should filter by user__username
object_list = self.model.objects.filter(user__username=username)
queryset = (Q(user__username=query))
It's nicely described here Many-to-one
CodePudding user response:
You can use contains so your query will look like this
if query:
queryset = (
Q(user__username__contains=query)|
Q(user__first_name__contains=query)|
Q(user__email__contains=query)
)
search_user = FriendList.objects.filter(queryset)
if not search_user:
messages.error(self.request, 'No user found.')