Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stefanfoulis/4276256 to your computer and use it in GitHub Desktop.
Save stefanfoulis/4276256 to your computer and use it in GitHub Desktop.
from django_select2.fields import AutoModelSelect2Field
from django_select2 import AutoHeavySelect2Widget
from django_select2.util import JSFunction
class MySearchWidget(AutoHeavySelect2Widget):
def init_options(self):
super(MySearchWidget, self).init_options()
self.options['formatResult'] = JSFunction('''function(object, container, query){return object.path + '<br/><strong style="font-size: 14px;">' + object.text + '</strong>';}''')
self.options['formatSelection'] = JSFunction('''function(object, container){return object.text;}''')
class MySearchField(AutoModelSelect2Field):
search_fields = ['title_set__title__icontains', 'title_set__menu_title__icontains', 'title_set__slug__icontains']
widget = MySearchWidget
def extra_data_from_instance(self, obj):
breadcrumbs = [p.get_menu_title() for p in obj.get_ancestors()]
path = u" | ".join(breadcrumbs)
return {
'path': path,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment