try:
    # Django <=1.9
    from django.db.models.loading import get_model
except ImportError:
    # Django 1.10+
    from django.apps import apps
    get_model = apps.get_model

from django.http import JsonResponse
from django.contrib.admin.views.decorators import staff_member_required


@staff_member_required
def filter_models(request):
    model_name = request.GET.get('model')
    search_field = request.GET.get('search_field')
    value = request.GET.get('q')

    model = get_model(model_name)

    values = model.objects.filter(**{'{}__icontains'.format(search_field): value})[:10]
    values = [
        dict(pk=value.pk, name=unicode(value))
        for value
        in values
    ]

    return JsonResponse(dict(result=values))
