| 1 |
_Queryutils App for Django_ |
|---|
| 2 |
|
|---|
| 3 |
This module is used to implement a very simple query/search language bound to |
|---|
| 4 |
some model for a webapp. The idea is to have a simple way in which users of a |
|---|
| 5 |
webapp can specify a search/query in some written form (a textbox). |
|---|
| 6 |
|
|---|
| 7 |
Suppose we have an Author Model, |
|---|
| 8 |
|
|---|
| 9 |
class Author (models.Model): |
|---|
| 10 |
|
|---|
| 11 |
first_name models.CharField() |
|---|
| 12 |
last_name = model.CharField() |
|---|
| 13 |
books = models.ManyToMany(Books) |
|---|
| 14 |
... |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
We want to allow the user to type something like this: "author:Arthur book:Space*" |
|---|
| 18 |
to retrieve all the books written by Arthur whose titles start with 'Space' |
|---|
| 19 |
|
|---|
| 20 |
Given a mapping of the keywords to some model's fields : |
|---|
| 21 |
|
|---|
| 22 |
'author' -> Author.first_name, Author.last_name |
|---|
| 23 |
'book' -> Author.books |
|---|
| 24 |
|
|---|
| 25 |
and a string of the form 'keyword1:value keyword2:value' this module allows you |
|---|
| 26 |
perform a query on the mapped fields of the model. The values given on the search |
|---|
| 27 |
string written by the user are the values that we are inquiring for. |
|---|
| 28 |
|
|---|
| 29 |
In short: |
|---|
| 30 |
|
|---|
| 31 |
1) Definie a mapping: |
|---|
| 32 |
|
|---|
| 33 |
'author' -> Author.first_name, Author.last_name |
|---|
| 34 |
'book' -> Author.books |
|---|
| 35 |
|
|---|
| 36 |
2) write a query: |
|---|
| 37 |
|
|---|
| 38 |
author:Arthur C. book:Space* |
|---|
| 39 |
|
|---|
| 40 |
3) use our module: |
|---|
| 41 |
|
|---|
| 42 |
SearchQueryGenerator.__init__(mapping) |
|---|
| 43 |
SearchQueryGenerator.make_query(query) |
|---|
| 44 |
|
|---|
| 45 |
4) result: |
|---|
| 46 |
|
|---|
| 47 |
Author.objects.filter((Q(first_name='Arthur') | Q(last_name='Arthur')) |
|---|
| 48 |
& Q(book_iregex='Space.*'))s |
|---|
| 49 |
|
|---|
| 50 |
For more details on how to use this read the module documentation. |
|---|
| 51 |
|
|---|
| 52 |
__How To Install__ |
|---|
| 53 |
|
|---|
| 54 |
Checkout the project to the directory where you keep your django third-party |
|---|
| 55 |
apps. (Or checkit out and add it to you PYTHONPATH) In settings.py of your |
|---|
| 56 |
project add to the INSTALLED_APPS variable: 'queryutils', |
|---|