Hello All, As a learning project, I am currently putting together a site to track all of the teams and players in the NBA along with their basic statistics and information. My two models so far are, as you might expect, players and teams. For the player model, I have separate database fields for the player's first name and last name. Setting it up this way just seemed like good practice for being able to sort on last name, etc., later on. However, now that I am trying to implement a simple player search function on the site, I am coming up against a wee problem.
I am using the following method (from here: http://www.djangobook.com/en/1.0/chapter07/) to allow for a simple search through my database for players based on their first and last names: from django.db.models import Q def search(request): query = request.GET.get('q','') if query: qset = ( Q(first_name__icontains=query) | Q(last_name__icontains=query) ) results = Player.objects.filter(qset).distinct() else: results = [] return render(request,'search.html', { 'results': results, 'query': query }) And then in my view (html file): <html> <form action="." method="GET"> <label for='q'>Player name: </label> <input type="text" name="q" value="{{ query|escape }}" /> <input type="submit" value="Search" /> </form> {% if query %} <h2>Search Results for "{{ query|escape }}":</h2> {% if results %} <ul> {% for player in results %} <li><a href='/players/{{player.id}}/'>{{ player|escape }}</a></li> </br> {% endfor %} </ul> {% else %} <p>No players matched the search.</p> {% endif %} {% endif %} </html> This has been working fine, EXCEPT that it only allows the user to search for a player's first name OR last name, and not their full name. I know that this is because the query is only looking into the individual model fields for results. In my models.py file I have a custom method defined that returns the player's full name by joining the first and last name fields for a given player together, but this doesn't help because currently, my search function will only look through model fields in the database. I know that one option is for me to add an additional field in my database for a player's "full name", but that seems to violate the DRY principle, and feels stupid. So it seems I have two other choices: 1) Make my search method able to search the results of custom model methods, rather than just model fields. So rather than just looking at player.first_name and player.last_name, it looks at those two fields AND the result of def name(self): return self.first_name+' '+self.last_name 2) Make my search method able to break up a multi-word input into individual words and return the results for any and all of these words found in the database. I have found a method that appears to do something like this here: http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap, but I don't understand what it does very well as the author doesn't explain it for newbs like me (I know he's using regexes to break up the search input, but after that I don't get it), and I don't know where to put the first bit of code he writes (I don't understand "copy/ paste the following snippet anywhere in your project"--does this mean in the views.py file, or in its own separate file which I then have to import as a module in the views.py file, or somewhere else?). Any help with this would be much appreciated--I feel like I'm 90% of the way there, but being able to search only for first OR last name (and not full name as well) seems really lame so I want to figure this out! thanks, Guillaume PS - I know I referenced an outdated version of the Django book, but if that's really an issue, please explain how I should update my code rather than just wagging your finger at me and linking to the latest docs, as the only reason I'm using this outdated Django book version is because I couldn't find anything about how to implement this kind of search in the latest docs! Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.