'Searches in the fields of the given related model and returns the
result as a simple string to be used by the jQuery Autocomplete plugin'

def foreignkey_autocomplete(self, request):

    query = request.GET.get('q', None)
    app_label = request.GET.get('app_label', None)
    model_name = request.GET.get('model_name', None)
    search_fields = request.GET.get('search_fields', None)
    object_pk = request.GET.get('object_pk', None)
    try:
        to_string_function = self.related_string_functions[model_name]
    except KeyError:
        if six.PY3:
            to_string_function = (lambda x: x.__str__())
        else:
            to_string_function = (lambda x: x.__unicode__())
    if (search_fields and app_label and model_name and (query or object_pk)):
        def construct_search(field_name):
            if field_name.startswith('^'):
                return ('%s__istartswith' % field_name[1:])
            elif field_name.startswith('='):
                return ('%s__iexact' % field_name[1:])
            elif field_name.startswith('@'):
                return ('%s__search' % field_name[1:])
            else:
                return ('%s__icontains' % field_name)
        model = models.get_model(app_label, model_name)
        queryset = model._default_manager.all()
        data = ''
        if query:
            for bit in query.split():
                or_queries = [models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')]
                other_qs = QuerySet(model)
                other_qs.query.select_related = queryset.query.select_related
                other_qs = other_qs.filter(reduce(operator.or_, or_queries))
                queryset = (queryset & other_qs)
            if self.autocomplete_limit:
                queryset = queryset[:self.autocomplete_limit]
            data = ''.join([(six.u('%s|%s\n') % (to_string_function(f), f.pk)) for f in queryset])
        elif object_pk:
            try:
                obj = queryset.get(pk=object_pk)
            except:
                pass
            else:
                data = to_string_function(obj)
        return HttpResponse(data)
    return HttpResponseNotFound()
