Skip to main content

AutoComplete for ForeignKey and ManyToManyField.

Project description

# AutoComplete for ForeignKey and ManyToManyField
uses same syntax as the search for django-admin

requirements
* query.js (http://jquery.com/)
* query.autocomplete.js

## Example


use a fairly simple
*models.py*
class Type (models.Model):
title = models.CharField()

class Celebrity(models.Model):
name = models.CharField()

class Film(models.Model):
type = models.ForeignKey( Type )
director= models.ManyToManyField( Celebrity, related_name="director")
actor = models.ManyToManyField( Celebrity, related_name="actor")

in m2m field need to specify related_name

*admin.py*
from apps.autocomplete.widgets import *

class FilmAdmin(AutocompleteModelAdmin):
related_search_fields = {

'type': ('title',),
'actor': ('^name',),
'director': ('^name',),
}
admin.site.register( Film, FilmAdmin )

"related_search_fields" parameter is used to specify on what fields you want to search 'actor' and 'director' ties are the names given in "related_name" query syntax is similar to [searching in admin panel](http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields)

I use the " 'actor': ('^name',) " operator ^ means the beginning of the field. and eventually will be formed about the substitution request form For example, if related_search_fieldsis set to ('^name',') and a user searches for john lennon, Django will do the equivalent of this SQL WHERE clause:

WHERE name ILIKE 'john%' AND name ILIKE 'lennon%'

Project details


Release history Release notifications

This version
History Node

0.0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django_autocomplete_foreignkey-0.0.1.tar.gz (11.7 kB) Copy SHA256 hash SHA256 Source None Oct 9, 2013

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page