Posted in 2009
I'm modeling game results in a Django app I'm working on. A team consists of one or more players, and players may be on multiple teams – a
ManyToManyField in Django. Then there are locations that maintain a list of players (not teams) who are authorized to play there, and players may be authorized for multiple locations – another
ManyToManyField. What I wanted to find was, given a location, a list of teams whose players are all authorized to play at that location.
If you happen to be one of about 4 people that actually reads this, you may notice I added a search box to the sidebar. What's far more interesting is the code behind it. I posted a snippet containing the code I wrote to accomplish this. Basically, it's just some convenience code to avoid having to drop down to SQL or use triggers for the simple case of having a model where you want certain fields to be full-text searchable. A
SearchableModel will, by default, automatically update it's search index when you call its
save() method. The API is documented at the top of the snippet, so I won't duplicate it here, but I will share the search view this site uses: