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 <https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.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
.. |Circle CI| image:: https://circleci.com/gh/gregplaysguitar/django-simple-search.svg?style=svg
:target: https://circleci.com/gh/gregplaysguitar/django-simple-search
.. |codecov| image:: https://codecov.io/gh/gregplaysguitar/django-simple-search/branch/master/graph/badge.svg
:target: https://codecov.io/gh/gregplaysguitar/django-simple-search
.. |Latest Version| image:: https://img.shields.io/pypi/v/django-simple-search.svg?style=flat
:target: https://pypi.python.org/pypi/django-simple-search/
Raw data
{
"_id": null,
"home_page": "https://github.com/gregplaysguitar/django-simple-search",
"name": "django-simple-search",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Greg Brown",
"author_email": "greg@gregbrown.co.nz",
"download_url": "https://files.pythonhosted.org/packages/ce/ce/6c07387b07de2e69710b8c313d426acbb735e8e99226dd230af58cea8cfc/django-simple-search-1.0.2.tar.gz",
"platform": "any",
"description": "Django simple search provides the same functionality and convenience\nthat ``search_fields`` does in the django admin.\n\nSee http://gregbrown.co.nz/code/django-simple-search/ for more details.\n\n|Circle CI| |codecov| |Latest Version|\n\nInstallation\n------------\n\nDownload the source from\nhttps://pypi.python.org/pypi/django-simple-search/ and run\n``python setup.py install``, or:\n\n::\n\n > pip install django-simple-search\n\nDjango 1.8 or higher is required.\n\nQuick start\n-----------\n\n::\n\n from simple_search import search_filter\n from .models import MyModel\n\n query = 'test'\n search_fields = ['^title', 'description', '=id']\n f = search_filter(search_fields, query)\n filtered = MyModel.objects.filter(f)\n\nFor convenience you can create a search form class via the provided\nfactory:\n\n::\n\n from .models import MyModel\n from simple_search import search_form_factory\n\n SearchForm = search_form_factory(MyModel.objects.all(),\n ['^title', 'description'])\n\nReference\n---------\n\n``simple_search.search_filter(search_fields, query)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nGiven a list of ``search_fields`` to search on and a query, return a\n``models.Q`` object which can be used to filter a queryset.\n\n``search_fields`` behaves exactly like the django admin\n`search\\_fields <https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields>`__\noption. Example:\n\n::\n\n search_fields = [\n # match from the start of the title field\n '^title',\n\n # match anywhere within the description field\n 'description',\n\n # match from the start of the related category's title field\n '^category__title',\n\n # exact match on object id\n '=id'\n ]\n\n``simple_search.search_form_factory(queryset, search_fields)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nCreate a search form class which will filter ``queryset`` according to\n``search_fields`` and the form field ``q``. Example:\n\n::\n\n # forms.py\n from .models import MyModel\n from simple_search import search_form_factory\n\n SearchForm = search_form_factory(MyModel.objects.all(),\n ['^title', 'description'])\n\n # views.py\n from django.shortcuts import render\n from .forms import SearchForm\n\n @render('search.html')\n def search(request):\n form = SearchForm(request.GET or {})\n if form.is_valid():\n results = form.get_queryset()\n else:\n results = MyModel.objects.none()\n\n return {\n 'form': form,\n 'results': results,\n }\n\nRunning tests\n-------------\n\nUse tox (https://pypi.python.org/pypi/tox):\n\n::\n\n > pip install tox\n > cd path-to/django-simple-search\n > tox\n\n.. |Circle CI| image:: https://circleci.com/gh/gregplaysguitar/django-simple-search.svg?style=svg\n :target: https://circleci.com/gh/gregplaysguitar/django-simple-search\n.. |codecov| image:: https://codecov.io/gh/gregplaysguitar/django-simple-search/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/gregplaysguitar/django-simple-search\n.. |Latest Version| image:: https://img.shields.io/pypi/v/django-simple-search.svg?style=flat\n :target: https://pypi.python.org/pypi/django-simple-search/\n",
"bugtrack_url": null,
"license": "BSD License",
"summary": "Recurring event tools for django",
"version": "1.0.2",
"project_urls": {
"Homepage": "https://github.com/gregplaysguitar/django-simple-search"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cece6c07387b07de2e69710b8c313d426acbb735e8e99226dd230af58cea8cfc",
"md5": "b54eb4a4517b867a5784d207b183b588",
"sha256": "5edaa08c610bf49f8b51de2cfe3e168457a821916e732c82c562ba1ab8ed4743"
},
"downloads": -1,
"filename": "django-simple-search-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "b54eb4a4517b867a5784d207b183b588",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4984,
"upload_time": "2017-03-20T21:18:10",
"upload_time_iso_8601": "2017-03-20T21:18:10.557486Z",
"url": "https://files.pythonhosted.org/packages/ce/ce/6c07387b07de2e69710b8c313d426acbb735e8e99226dd230af58cea8cfc/django-simple-search-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2017-03-20 21:18:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gregplaysguitar",
"github_project": "django-simple-search",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"tox": true,
"lcname": "django-simple-search"
}