Pull requests

#5 Declined
Repository
evildmp evildmp
Branch
search-fields-version-2
Repository
spookylukey spookylukey
Branch
search-fields-version-2

A much-improved attempt to provide search fields (not ready to merge)

Author
  1. evildmp
Reviewers
Description

This is still not ready to merge (no docs or test, and a bit rough and ready) but I think it's the correct approach.

Define your filter

class NewsArticleFilterSet(FilterSet):
    fields = [
        ('hosted_by', {
            "template": "django_easyfilters/text_search.html",
            "search_key": "hosted_by__name__icontains",
            "filter_type": "search",
            }
            ),
        ('title', {
            "template": "django_easyfilters/text_search.html",
            "search_key": "title__icontains",    
            "filter_type": "search",
            }
            ),
        'date',
        ]

A filter class is created with three new arguments:

        filter_type="default",
        template="django_easyfilters/default.html",
        search_key=None, # defaults to self.field if not provided

My example

class NewsArticleFilterSet(FilterSet):
    fields = [
        ('hosted_by', {
            "template": "django_easyfilters/text_search.html",
            "search_key": "hosted_by__name__icontains",
            "filter_type": "search",
            }
            ),
        ('title', {
            "template": "django_easyfilters/text_search.html",
            "search_key": "title__icontains",    
            "filter_type": "search",
            }
            ),
        'date',
        'importance',  
        ]

The first example is a ForeignKey search, so I have told it what to do about that in its search_key.

Templates

View template

My view renders to:

{% load pagination_tags %}

{% autopaginate newsarticles 10 %}

<form action="" method="GET">
    {{ newsarticlesfilter }}
</form>

{% paginate %}

{% for newsarticle in newsarticles %}
  <div>
    <h2>{{ newsarticle.title }}</h2>
    <div><b>Please contact:</b>
    {% for person in newsarticle.please_contact.all %}
      {{ person }}{% if not forloop.last %},{% endif %}
    {% endfor %}
    <br><b>Published by:</b> {{ newsarticle.hosted_by }}</div>
  </div>
{% endfor %} 

(for now).

django-easyfilters templates

Are in the application's templates folder. One's the default, one's for text searches. It ought to be easy to override them.

So, comments? (Speaking of comments, I will add more to the code and push again.)

Comments (2)

  1. Luke Plant repo owner

    I've got a solution that doesn't require easyfilters being modified, which is good because this seems like a very poor fit. See my comment on the ticket:

    https://bitbucket.org/spookylukey/django-easyfilters/issue/5/text-search-filter#comment-4796373

    I'm going to close the pull request for that reason. Sorry not to have got this to you earlier - I did mention on the ticket that I thought this could be done entirely with view/template code, outside of easyfilters.