I'm trying to build a search system, and I want to search by multiple fieldsname, state, city,
in my django models. I wrote the below code, yet I've been unable to figure out how to go about it.
Models:
class Finhall(models.Model):
user=models.ForeignKey(User)
name=models.CharField(max_length=250, unique=True)
address=models.CharField(max_length=200)
city=models.CharField(max_length=200)
state=models.CharField(max_length=200)
def __unicode__(self):
return u'%s' % (self.name)
Views.py
def hup_find(request):
if ('q' in request.GET) and request.GET['q'].strip():
query_string=request.GET.get('q')
seens=Finhall.objects.filter(name__icontains=query_string)
else:
seens=None
return render_to_response('find.html',{'seens':seens},context_instance=RequestContext(request))
Template:
{% block content %}
<body>
<form action="" method="GET">
<input type="text" name="q" />
<button type="submit">search</button>
</form>
{% for seen in seens %}
<p> {{seen.name}}</p>
{% empty %}
<p> no search </p>
{% endfor %}
</body>
{% endblock %}
How can I go about this? I don't want to use haysatck due to some personal reasons.
you can use django Q
objects to do OR
query,
or if you want to AND
your queries together just use the current lookups as kwargs
seens = Finhall.objects.filter(
name__icontains=query_string,
address__icontains=query_string
)
You should really consider full text search or haystack
(which makes search easy) because icontains
issues a %LIKE%
which is not remotely scalable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With