feature type ajax-select (with custom lookup class)

This commit is contained in:
Sanj 2011-08-31 01:02:30 +05:30
parent 86b2e2f597
commit 9232052135
2 changed files with 23 additions and 0 deletions

View File

@ -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')

View File

@ -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