from django.db.models.fields import FieldDoesNotExist, CharField
from django.conf import settings


def get_search_fieldname(model):
    # If model has field 'title' then use that, else use the first
    # CharField on model.
    fieldname = ''
    try:
        model._meta.get_field_by_name('title')
        fieldname = 'title'
    except FieldDoesNotExist:
        for field in model._meta.fields:
            if isinstance(field, CharField):
                fieldname = field.name
                break
    if not fieldname:
        raise RuntimeError("Cannot determine fieldname")
    return fieldname


def get_threshold_for_model(model):
    key = '%s.%s' % (model._meta.app_label, model._meta.module_name)
    return getattr(settings, 'SIMPLE_AUTOCOMPLETE', {}).get(
        key, {}).get('threshold', None
    )


def get_setting(app_label_model, key, default):
    return getattr(settings, 'SIMPLE_AUTOCOMPLETE', {}).get(app_label_model, {}).get(key, default)

@@ -239,20 +239,336 @@
 e = 
-''%0A    try:%0A
+get_setting(%22%25s.%25s%22 %25 (model._meta.app_label, model.__name__.lower()), %5C%0A%09'search_field', '')%0A    if fieldname:%0A%09try:%0A%09    model._meta.get_field_by_name(fieldname)%0A%09except FieldDoesNotExist:%0A%09    raise RuntimeError(%22Field '%25s.%25s' does not exist%22 %25 (model._meta.app_label, %5C%0A%09%09model.__name__.lower()))%0A    else:%0A        try:%0A    
     
@@ -610,32 +610,36 @@
 title')%0A        
+   
+ 
 fieldname = 'tit
@@ -642,16 +642,20 @@
 'title'%0A
+    
     exce
@@ -684,16 +684,20 @@
         
+ 
+   
 for fiel
@@ -733,16 +733,20 @@
         
+  
+  
 if isins
@@ -778,32 +778,36 @@
                 
+    
 fieldname = fiel
@@ -813,16 +813,20 @@
 ld.name%0A
+    
         

add search_field settings