[Answer]-How to generate a dynamic search form for Django?

1đź‘Ť

@yegle

I’d suggest to use this media type: http://code.ge/media-types/collection-next-json/ it’s an extension of the: http://amundsen.com/media-types/collection/format/ media type and you can achieve your goal without problems.

Look at this document:

{"collection": {
  "version": 1.0,

  "queries": [
    {
      "href":    "http://service.com/my-resource", 
      "rel":     "search", 
      "prompt":  "Enter search string", 
      "data" : [
        {
          "name": "query",
          "prompt": "Search query",
          "required": true
        }, {
          "name":   "gender",
          "prompt": "Gender",
          "list":   {
            "default": "female",
            "options": [
              {"value": "female", "prompt": "Female"},
              {"value": "male",   "prompt": "Male"}
            ]
          }
        }
      ]
    }
  ]  

}}

with “queries” array you can describe several search templates which easily can be transformed to the HTML form. In each query object you can define:

  • Search URI
  • Title of the “form”(i.e. prompt); and
  • Any number of query parameters with different requirements.

As you see from example above there are two query parameters – “query” and “gender”, of which “query” parameter can be converted to HTML.input and “gender” parameter to HTML.select.

I hope I correctly understood your question and goal.

👤ioseb

0đź‘Ť

I am not sure about django tools.However,you can use MYSQL’s full text boolean search to achieve most of the above requirements.Refer the below link and revert back with your comments!

http://dev.mysql.com/doc/refman//5.5/en/fulltext-boolean.html

👤Vivek S

0đź‘Ť

Have a look at django-filter

Or, in some hacking way, use Django Admin. You could customize a ModelAdmin for the target model and use ChangeList to deal w/ querystring. Take built-in User for example:

from django.contrib.admin.options.IncorrectLookupParameters
from django.contrib.admin.views.main import ChangeList
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
self = UserAdmin(User, None) # or taken from admin.site._registry if the model admin is registered
cl = ChangeList(request, User, self.list_display,
             self.list_display_links, self.list_filter, self.date_hierarchy,
             self.search_fields, self.list_select_related,
             self.list_per_page, self.list_max_show_all, self.list_editable,
             self)
# then cl.get_query_set would process querystring and generate the queryset
try:
    cl.get_query_set(request)
except IncorrectLookupParameters:
    'encounter an invalid lookup'
👤okm

Leave a comment