Text search filter

evildmp avatarevildmp created an issue

What would be required to create a text search filter? In http://v029.medcn.uwcm.ac.uk:8000/news-and-events/django_easy_filter/ for example (that demo URL is not going to be around indefinitely), it would be useful to search on title and summary fields.

In this case, the user would not be presented with a list of choices, but a text box, and and the add/remove options.

Comments (8)

  1. Luke Plant

    Is it possible to add this logic inside the view function? FilterSet.qs returns a queryset that can be manipulated in the normal way, so you can layer the filtering done by the FilterSet with any other search functionality. So you could have a normal search input box, with a 'Search' button and a 'Clear' button. I think it should be possible to do this using normal HTML links/forms, without javascript.

    <form action="" method="GET">
      <input type=text name="search" value="{{ searchterms }}">
      <input type=submit name="searchbtn" value="Search">
      <input type=submit name="clearbtn" value="Clear">
    </form>
    

    Then in the view

    books = Book.objects.all()
    
    if 'clearbtn' in request.GET:
        searchterms = None
    else:
        searchterms = request.GET.get('search', None)
    
    if searchterms:
       books = do_search(books)
    
    booksfilter = BookFilterSet(books, request.GET)
    
    return render(request, "booklist.html", 
      {'books': booksfilter.qs,
       'booksfilter': booksfilter,
       'searchterms': searchterms,
      })
    

    If that works (and I haven't thought it through fully), that seems like a cleaner solution.

  2. evildmp

    Yes in fact it does, I have a very crude version working.

    At this point, I think that the template that's a property of FilterSet perhaps needs to be moved from https://bitbucket.org/spookylukey/django-easyfilters/src/4bad68d3b0270c62a2e491bfa4edd8e86f0858c8/django_easyfilters/filterset.py?at=default#cl-35.

    In fact the template for each filter should be associated with the appropriate Filter subclass, allowing different filter types to have different templates when required.

    In the case of the different template for a text search filter, the Filter subclass should be the same (i.e. it depends on the kind of field), but it should be possible to instantiate each one with an optional way of presenting it to the user.

    I'll take a look at that this evening.

  3. evildmp

    Do you have a few minutes to chat about this on IRC? I think I have got as far as I can with the approach I started with. Probably it will be better now to start again from scratch, but it would be a great help to talk it over for a minute.

  4. Luke Plant

    I have an example which implements this without needing to use/patch easyfilters at all.

    View:

    https://bitbucket.org/spookylukey/django-easyfilters/src/0a3b56322d19f63622c38f5b601a09d099b275a4/django_easyfilters/tests/views.py?at=default#cl-39

    Template:

    https://bitbucket.org/spookylukey/django-easyfilters/src/0a3b56322d19f63622c38f5b601a09d099b275a4/django_easyfilters/tests/templates/book_search.html?at=default

    Since the UI and mechanism for text search is quite different from clickable filters, I think this is much better implemented in this way. The only tricky thing is generating the hidden inputs so that query parameters are propagated, but it is a few lines of template code.

    Sorry I wasn't able to give this input before.

  5. evildmp

    The version at https://bitbucket.org/spookylukey/django-easyfilters/pull-request/5/a-much-improved-attempt-to-provide-search/ allows this sort of thing:

        fields = [
            ('title', {
                "template": "django_easyfilters/text_search.html",
                "search_keys": [
                    "title__icontains", 
                    "summary__icontains",
                    ],    
                "filter_type": "search",
                "field_name": "Title/summary",
                }
                ),
            ]
    

    where a search field can apply to more than one model field (e.g.. search for "Jane" in authors or titles), and permits different search types to be set up through the same interface.

    I can see that there's some advantage in keeping the search types separate, though.

  6. evildmp

    How do you feel about moving the template to a file, just to make it easier to override by providing one in the project (or elsewhere)?

  7. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.