[Answer]-Django-selectable: Show only distinct autocompletions

1👍

If you aren’t interested in selecting a Material instance and you only want to select the text matching the prerequisites field then you can override get_query https://django-selectable.readthedocs.org/en/version-0.7.0/lookups.html#lookup-api

class PrerequisitesLookup(ModelLookup):
  """Prerequisites"""
  model=Material
  search_fields = ('prerequisites__istartswith', )

  def get_query(self, request, term):
    qs = super(PrerequisitesLookup, self).get_query(request, term)
    return qs.values_list('prerequisites', flat=True).distinct()

  def get_item_id(self, item):
    return item

registry.register(PrerequisitesLookup)

Note that the item in the various formatting methods (get_item_label, get_item_id, get_item_value) will be the prerequisites string rather than a Material instance which is why the default get_item_id has to be overridden.

Leave a comment