简体   繁体   中英

Django Haystack add additional input to search

I've a haystack search currently works well searching on the following model:

class AdminEntry(models.Model):
    product = models.ForeignKey('Product')
    number_entries = models.IntegerField(max_length=3,  null=True)

My search_indexes.py :

class AdminEntryIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    author = CharField(model_attr='product__author')
    title = CharField(model_attr='product__title')
    desc = CharField(model_attr='product__desc')

    def get_queryset(self):
        return AdminEntry.objects.all()

site.register(AdminEntry, AdminEntryIndex)

but now I want to add additional search parameter in dropdown in my search form with 2 values ['Admin', 'Staff'] since I've added another model:

class StaffEntry(models.Model):
    product = models.ForeignKey('Product')
    number_entries = models.IntegerField(max_length=3,  null=True)

I want to my search to search on StaffEntry if the dropdown selected is 'Staff', and AdminEntry is 'Admin' is selected. Can someone help me on how to achieve this using Haystack with Whoosh? Thanks in advance.

What you're after is the ModelSearchForm:

http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#modelsearchform

use this instead of the default SearchForm, and you'll get checkboxes for each indexed model. If you extend the form you'll be able to change it to a select instead of checkboxes.

See http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#views for information on how to use your custom form in the view.

forms.py

class Search(SearchForm):

         OPTIONS = [
                ('staff','Staff'),
                ('admin','Admin')
               ]

          option = forms.ChoiceField(
                    widget=forms.Select(
                        attrs={'class':'cd-select','id':'cd-dropdown'}),
                        label='',
                        choices=OPTIONS,
                        )
         def __init__(self, *args, **kwargs):
             super(Search, self).__init__(*args, **kwargs)
             self.initial['option'] = 'staff'
             self.fields['q'].label = ''



        def search(self):
            if not self.is_valid():
                 return self.no_query_found()
            if not self.cleaned_data.get('q'):
                 return self.no_query_found()
            sqs = self.searchqueryset.auto_query(self.cleaned_data['q'])

            if self.cleaned_data['option'] == 'staff':
                 sqs = SearchQuerySet().auto_query(self.cleaned_data['q']).models(Staff)
            elif self.cleaned_data['option'] == 'admin':
                 sqs = SearchQuerySet().auto_query(self.cleaned_data['q']).models(Admin)
            if self.load_all:
                 sqs = sqs.load_all()
            return sqs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM