On Wed, Apr 22, 2009 at 10:48 AM, tyrrrr <electioneer...@gmail.com> wrote:
> > this is the error: > > reduce() of empty sequence with no initial value > > Request Method: GET > Request URL: http://dsfffffffffffffffffffff/search/ > Exception Type: TypeError > Exception Value: reduce() of empty sequence with no initial value > Exception Location: /dsfffffffffffffffffffff/models.py in search, > line 59 > > views.py in searching > 68. results = Entry.objects.search(query_string) > > models.py in search > 59. return qs.filter(reduce(operator.or_, q_objects)) > The error implies your q_objects is an empty sequence: >>> import operator >>> reduce(operator.or_, ()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: reduce() of empty sequence with no initial value Which, looking at your code, seems to be exactly what you'd expect for a url that has no q in request.GET. You can't call reduce on an empty sequence, so change your code to properly account for when it isn't given anything to search for. Karen > On 22 Apr, 09:17, tyrrrr <electioneer...@gmail.com> wrote: > > Hi > > I am trying to add a search view to an application I wrote in django > > but my EntryManager() seems to be getting hung up on the last line of > > code when django tries to call the url of the search. > > > > This EntryManager() (models.py) is excellent for performing searches > > in the django shell ($ python manage.py shell) like > > > > x = Entry.objects.search('spam') > > list(x) > > > > for example > > > > However when I attempt to visit the url of the search I am met with > > the error " reduce() of empty sequence with no initial value " and a > > reference to the last line of EntryManager() > > > > return qs.filter(reduce(operator.or_, q_objects)) > > I use this method to strip multiple word queries and place underscores > > between the words > > > > type error: > > reduce() of empty sequence with no initial value > > > > The problems seems to be that the EntryManager() requires a string > > from the beginning and I haven't been able to make it work without > > one. > > > > The rest of my code works perfectly. > > > > Obviously the value will be empty before a query is made. What am I > > doing wrong? Is there perhaps a better way to connect my search view > > to this object? > > > > ps: I am aware that this isn't exactly a very efficient, scalable or > > long-term solution to my search issue. It is however important that I > > get this work as a temporary solution before I move to whoosh/ > > haystack. > > > > Thanks everyone! > > > > ////////////////////// > > models.py > > > > import operator > > from django.db import models > > from django.db.models import Q > > > > class EntryManager(models.Manager): > > def search(self, search_terms): > > terms = [term.strip() for term in search_terms.split > > ()] > > q_objects = [] > > for term in terms: > > q_objects.append(Q(name__icontains=term)) > > q_objects.append(Q > > (description__icontains=term)) > > qs = self.get_query_set() > > return qs.filter(reduce(operator.or_, q_objects)) > > > > ////////////////////// > > views.py > > > > def searching(request): > > query_string = '' > > if ('q' in request.GET) and request.GET['q'].strip(): > > query_string = request.GET['q'].strip() > > results = Entry.objects.search(query_string) > > return render_to_response("search_results.html", > > {'query_string': query_string, 'results' : results}) > > > > ////////////////////// > > search_results.html > > > > <div>Searched Results</div> > > > > {%if results%} > > {% for e in results%} > > <a href=../{{e.slug}}>{{e.name}}</a></div> > > {% endfor %} > > {% else %} Nothing to see here... > > {% endif %} > > > > ////////////////////// > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---