Here is my code for the AutomaticSearchManipulator:

from django.db.models.manipulators import *
from django.db.models.query import Q

class AutomaticSearchManipulator (AutomaticManipulator):
     """
     A generic manipulator for searching. It does all the work for  
"query by
     example" queries.

     Typical view usage:

     def address_search(request):
         Address.add_to_class('SearchManipulator',  
AutomaticSearchManipulator)

         manipulator = Address.SearchManipulator()
         if request.GET:
             new_data = request.GET.copy()
             manipulator.do_html2python(new_data)
             query_set = manipulator.search(new_data)
             return render_to_response('address_list.html',  
{'object_list': query_set})
         else:
             errors = new_data = {}

         form = forms.FormWrapper(manipulator, new_data, errors)
         return render_to_response('address_search.html', {'form':  
form})
     """
     change = False

     SEARCH_TYPES = {
         'iexact': '=',
         'exclude': '≠',
         'istartswith': 'starts with',
         'iendswith': 'ends with',
         'icontains': 'contains',
         'gt': '>',
         'gte': '>=',
         'lt': '<',
         'lte': '<=',
         'in': 'one of',
         'range': 'range of',
     }

     def search_types(self):
         """
         Generate the search type drop-down widget. Currently doesn't  
work,
         need to figure out how to adjust the name of the widget to  
correspond
         with the field.

         Meant to be used in forms (added as an extra context) to  
allow more
         flexible searching.
         """
         output = ['<select id="is_%s_st" name="%s_st">' % ()]
         str_data = str(data) # normalize to string
         for value, display_name in self.SEARCH_TYPES:
             selected_html = ''
             if str(value) == str_data:
                 selected_html = ' selected="selected"'
             output.append('    <option value="%s"%s>%s</option>' % \
                          (escape(value), selected_html, escape 
(display_name)))
         output.append('  </select>')
         return '\n'.join(output)

     def save(self, new_data):
         """
         Does nothing. Doesn't make sense to have a save method.
         Raise NotImplementedError
         """
         raise NotImplementedError

     def search(self, new_data, default_search='iexact'):
         """
         Search for the object or objects specified by new_data.
         """

         where_clause = {}
         posted_values = new_data.lists()
         for k, v in posted_values:
             if len(v) > 1:
                 where_clause['%s__in' % k] = v
             else:
                 if v[0]:
                     where_clause['%s__%s' % (k, default_search)] = v[0]
         if where_clause:
             q = Q(**where_clause)
             return self.manager.filter(q)
         else:
             return self.manager.all()



On Aug 20, 2006, at 2:51 PM, Corey wrote:

>
> I had a need to provide a "Search by example" form, and I thought that
> a new type of manipulator similar to an AddManipulator and
> ChangeManipulator.
>
> I've started the code, and would be happy to submit when I'm done if
> you guys/glas think it Django-ish enough.
>
> The idea is that a view to handle searching could look like:
>
> def address_search(request):
>     manipulator = Address.SearchManipulator()
>     if request.GET:
>         new_data = request.GET.copy()
>         manipulator.do_html2python(new_data)
>         query_dict = manipulator.search(new_data)
>         return render_to_response('address_list.html', {'query_dict':
> query_dict})
>     else:
>         errors = new_data = {}
>
>     form = forms.FormWrapper(manipulator, new_data, errors)
>     return render_to_response('address_search.html', {'form': form})
>
> Thanks,
>
> Corey
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@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-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to