From 9232052135598302b534ed306aaf2e3d62e65292 Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 31 Aug 2011 01:02:30 +0530 Subject: [PATCH] feature type ajax-select (with custom lookup class) --- gazetteer/places/lookups.py | 22 ++++++++++++++++++++++ gazetteer/settings.py | 1 + 2 files changed, 23 insertions(+) create mode 100644 gazetteer/places/lookups.py diff --git a/gazetteer/places/lookups.py b/gazetteer/places/lookups.py new file mode 100644 index 0000000..db1c86f --- /dev/null +++ b/gazetteer/places/lookups.py @@ -0,0 +1,22 @@ +from places.models import FeatureType +from django.db.models import Q + +class FeatureTypeLookup(object): + + def get_query(self,q,request): + """ return a query set. you also have access to request.user if needed """ + return Contact.objects.filter(Q(code__icontains=q) | Q(name__icontains=q)) + + def format_result(self,ftype): + """ the search results display in the dropdown menu. may contain html and multiple-lines. will remove any | """ + return u"%s: %s" % (ftype.code, ftype.name,) + + def format_item(self,contact): + """ the display of a currently selected object in the area below the search box. html is OK """ + return unicode(contact) + + def get_objects(self,ids): + """ given a list of ids, return the objects ordered as you would like them on the admin page. + this is for displaying the currently selected items (in the case of a ManyToMany field) + """ + return FeatureType.objects.filter(pk__in=ids).order_by('code') diff --git a/gazetteer/settings.py b/gazetteer/settings.py index 06ba15d..31aaa7b 100644 --- a/gazetteer/settings.py +++ b/gazetteer/settings.py @@ -88,6 +88,7 @@ AJAX_LOOKUP_CHANNELS = { # the simplest case, pass a DICT with the model and field to search against : 'authority_record' : dict(model='places.authorityrecord', search_field='preferred_name'), 'time_frame': dict(model='places.timeframe', search_field='description'), + 'feature_type': ('places.lookups', 'FeatureTypeLookup'), # this generates a simple channel # specifying the model Track in the music app, and searching against the 'title' field