On 11 juin, 16:41, Adam <[EMAIL PROTECTED]> wrote: > I'm new to Django, and as a learning exercise I've been putting > together a simple contact database. There are separate fields for > first_name and last_name, and another field for the company name > called ac_name > > In the template for the main page I put a form that allows searching > by name or company, here's my code: > > <form action="/contacts/search/" method="post"> > <input type="text" name="search_parameter" id="search_parameter"><br / > > <input type="radio" name="choice" id="choice1" value="1" checked> > <label for="choice1"">by Name</label> > <input type="radio" name="choice" id="choice2" value="2"> > <label for="choice2"">by Company</label><br /> > <input type="submit" value="Go" /> > </form> > > And here is the relevant section of the views.py file: > > def search(request): > find_me = request.POST['search_parameter'] > sort_by_choice = request.POST['choice'] > if sort_by_choice == '1': > all_contacts = > Contact.objects.filter(last_name__icontains=find_me) > elif sort_by_choice == '2': > all_contacts = > Contact.objects.filter(ac_name__icontains=find_me) > return render_to_response('contactsdb/index.html', {'all_contacts': > all_contacts}) > > This currently works quite nicely as long as you only enter someone's > last name or company name, but if someone enters a person's full name > in the search box, it obviously returns nothing. I'm having trouble > figuring out how to search a person's full name for matching text.
The SimplestThinkThatCouldPossiblyWork (while certainly not the smartest) would be to add a full_name field to the Contact model, hidden in the admin, and autopopulated from the first_name and last_name fields whenever the Contact instance is saved. This last point can be adressed overriding the save() method of the Contact model, ie def save(self): self.full_name = "%s %s" % (self.first_name, self.last_name) super(Contact, self).save() HTH --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---