Skip to main content

Django application providing Ajax search capabilities.

Project description

Yaaac is lightweight Django application providing Ajax search to admin foreign-key form fields in addition to the raw_id_fields related lookup and - cerise sur le gateau - it is usable outside the admin.

Examples

In the admin

Configure the admin form to enable Ajax search where you usually use raw_id_fields.

Ajax search field

Start typing to select a value from the suggestion.

Ajax search in progress

The selected object is displayed using the unicode value. You can reset the field to choose another one.

Ajax search completed

In inlines as well.

Ajax search in inlines

Installation

Install the app from the source:

python setup.py build && (sudo) python setup.py install

Or with pip:

pip install django-yaaac

Add the app in your settings.INSTALLED_APPS:

INSTALLED_APPS = [
    ...,
    "django_yaaac",
    ...,
]

In the urls.py module of your project, define the url pattern for ajax calls:

import django_yaaac as yaaac
from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^yaaac/', include(yaaac.autocomplete.urls)),
    ...
)

Usage

What you need to do is to declare a custom ModelForm and use it in your ModelAdmin:

from django import forms
from django.contrib import admin
from django.contrib.admin.templatetags.admin_static import static
from django.template import RequestContext
from django_yaaac.forms.fields import AutocompleteModelChoiceField
from test_app import models


class BandMemberForm(forms.ModelForm):
    band = AutocompleteModelChoiceField(site=admin.site,
                                        queryset=models.Band.objects.all(),
                                        yaaac_opts={
                                            "search_fields": ["^name"],
                                            "suggest_by": "get_full_name",
                                            "min_chars": 3,    # Fire search when 3 chars are sent (1 by default.)
                                            "max_height": 400, # 300px by default.
                                            "width": 250,      # 300px by default.
                                        },
                                        required=True)
    class Meta:
        model = models.BandMember


class BandMemberAdmin(admin.ModelAdmin):
    form = BandMemberForm

    class Media:
        # You need jQuery.
        js = (static('js/jquery.min.js'), )


admin.site.register(models.BandMember, BandMemberAdmin)

The site parameter of AutocompleteModelChoiceField is required for related lookup (the magnifier glass). The search_fields is a list of fields to search against using the same syntax as in Django Admin (^, $ etc). Extra options min_chars, max_height and width are the counter-part of minChars, maxHeight and width in Autocomplete options.

Do not forget to add jQuery in your template (using ModelAdmin.Media.js in the example above). Outside the admin, you have to explicitly call the yaaac static files like that:

<head>
  {{ form.media }}
</head>

suggest_by is optional. It can be a field or a method of the model. By default, suggestions are shown using __unicode__ method.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-yaaac-0.92.2.tar.gz (646.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page