Has anyone successfully done this?
Trying to replicate functionality similar to what is found at
http://www.walmart.com/cservice/ca_storefinder.gsp
Where the user can enter either a city, state or zipcode and have an intelligent lookup
I started an attempt using a bit of regex and splitting fields though I quickly realized this was a task that would take more than a little bit of thought to make it work intelligently for the end user
approaches considered
- live ajax auto-complete calls
- Sphinx full text search
- regex search db side
- regex validate user input
snippet thus far below
import re
from general.models import ZipCode
def findLocation(value):
match = re.search(r"\d{5}", value)
if match:
try:
return Zipcode.objects.get(zip=value)
except ZipCode.DoesNotExist:
return False
else:
kwargs = {}
vals = value.split(',')
if len(vals) > 1:
kwargs['city'] = value[:len(value)-len(vals[-1])-1]
state = vals[-1].strip()
if len(state) == 2:
kwargs['state'] = state
else:
kwargs['state_name'] = state
else:
kwargs['city'] = value
return ZipCode.objects.filter(**kwargs)
ZipCode in this example is a database model that contains a all US zip codes / states / state abbreviations / cities
there is a copy available on github at
For this kind of advanced feature, you’d better use some dedicated tool, like the Solr search engine.
If you don’t know it, it’s a java based, open-source, very powerful search engine, with the ability to add location search features. The search actions will be performed through a web service (xml, json, etc.)
The steps are basically the following: you install solr on a server, you configure a schema (a way to store and index data), import data from you database, and bind your search form to the web service.
You might want to read this article to have more informations about geolocation searches (maybe slightly outdated, you’ll have to check).