I would like to be able to search for the user and have it direct me to a list of all users.
When I try the search function, I receive this error: Related Field got invalid lookup: icontains.
It traces back to this line:
clients = LessonCount.objects.filter(user__icontains=q)
Any ideas? Thanks in advance!
views.py:
def search(request):
try:
q = request.GET.get('q')
except:
q = None
if q:
clients = LessonCount.objects.filter(user__icontains=q)
context = {
'query': q,
'clients': clients
}
template = 'clients.html'
else:
context = {}
template = 'contact.html'
return render(request, template, context)
models.py:
class LessonCount(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
lesson_current_amount = models.PositiveIntegerField(default=0, verbose_name='Current Number of Lessons')
def __unicode__(self):
return str(self.user.username)
class Meta:
ordering = ['-user']
def update_lesson_count(self):
if self.lesson_current_amount > 0:
self.user.is_member = True
self.user.save()
elif self.lesson_current_amount <= 0:
self.user.is_member = False
self.user.save()
else:
pass
(user__icontains=q)
is calling the icontains lookup on the user object. I think what you should be doing is (user__username__icontains=q)
which will perform the lookup on the user's name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.