Coder Social home page Coder Social logo

gregplaysguitar / django-simple-search Goto Github PK

View Code? Open in Web Editor NEW
27.0 2.0 4.0 25 KB

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

License: Other

Python 100.00%
django python search

django-simple-search's Introduction

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

pip install django-simple-search

Or download the source from https://pypi.python.org/pypi/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

django-simple-search's People

Contributors

gregplaysguitar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

django-simple-search's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.