Skip to main content

Django simple search provides the same functionality and convenience that search_fields does in the django admin.

See http://gregbrown.co.nz/code/django-simple-search/ for more details.

Circle CI codecov Latest Version

Installation

Download the source from https://pypi.python.org/pypi/django-simple-search/ and run python setup.py install, or:

> pip install django-simple-search

Django 1.8 or higher is required.

Quick start

from simple_search import search_filter
from .models import MyModel

query = 'test'
search_fields = ['^title', 'description', '=id']
f = search_filter(search_fields, query)
filtered = MyModel.objects.filter(f)

For convenience you can create a search form class via the provided factory:

from .models import MyModel
from simple_search import search_form_factory

SearchForm = search_form_factory(MyModel.objects.all(),
                                 ['^title', 'description'])

Reference

simple_search.search_filter(search_fields, query)

Given a list of search_fields to search on and a query, return a models.Q object which can be used to filter a queryset.

search_fields behaves exactly like the django admin search_fields option. Example:

search_fields = [
    # match from the start of the title field
    '^title',

    # match anywhere within the description field
    'description',

    # match from the start of the related category's title field
    '^category__title',

    # exact match on object id
    '=id'
]

simple_search.search_form_factory(queryset, search_fields)

Create a search form class which will filter queryset according to search_fields and the form field q. Example:

# forms.py
from .models import MyModel
from simple_search import search_form_factory

SearchForm = search_form_factory(MyModel.objects.all(),
                                 ['^title', 'description'])

# views.py
from django.shortcuts import render
from .forms import SearchForm

@render('search.html')
def search(request):
    form = SearchForm(request.GET or {})
    if form.is_valid():
        results = form.get_queryset()
    else:
        results = MyModel.objects.none()

    return {
        'form': form,
        'results': results,
    }

Running tests

Use tox (https://pypi.python.org/pypi/tox):

> pip install tox
> cd path-to/django-simple-search
> tox

Metadata

Release files for django-simple-search 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-simple-search 1.0.2
File Size Uploaded Provenance
django-simple-search-1.0.2.tar.gz 5.0 kB Without provenance Details

Release files / django-simple-search-1.0.2.tar.gz

Download URL django-simple-search-1.0.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5edaa08c610bf49f8b51de2cfe3e168457a821916e732c82c562ba1ab8ed4743
BLAKE2b-256 checksum
How to use checksums
cece6c07387b07de2e69710b8c313d426acbb735e8e99226dd230af58cea8cfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-simple-search 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page