django simple approach to multi-field search

Posted by Scott Willman on Stack Overflow See other posts from Stack Overflow or by Scott Willman
Published on 2010-04-28T03:48:28Z Indexed on 2010/04/28 3:53 UTC
Read the original article Hit count: 242

Filed under:
|
|

I have a simple address book app that I want to make searchable. The model would look something like:

class Address(models.Model):
    address1 = models.CharField("Address Line 1", max_length=128)
    address2 = models.CharField("Address Line 2", max_length=128)
    city     = models.CharField("City", max_length=128)
    state    = models.CharField("State", max_length=24)
    zipCode  = models.CharField("Zip Code", max_length=24)

    def __unicode__(self):
        return "%s %s, %s, %s, %s" % (self.address1, self.address2, self.city, self.state, self.zipCode)

class Entry(models.Model):
    name      = models.CharField("Official School Name", max_length=128)
    createdBy = models.ForeignKey(User)
    address   = models.ForeignKey(Address, unique=True)

    def __unicode__(self):
        return "%s - %s, %s" % (self.name, self.address.city, self.address.state)

I want the searching to be fairly loose, like: Bank of America Los Angeles 91345. It seems like I want a field that contains all of those elements into one that I can search, but that also seems redundant. I was hoping I could add a method to the Entry model like this:

def _getSearchText(self):
    return "%s %s %s" % (self.name, self.address, self.mascot)
searchText = property(_getSearchText)

...and search that as a field, but I suppose that's wishful thinking... How should I approach this using basic Django and SqLite (this is a learning exercise).

Thank you!!

© Stack Overflow or respective owner

Related posts about django

Related posts about search