From e7178b01c25ca4d31804e6ab0da2a83f335d7482 Mon Sep 17 00:00:00 2001 From: Shivaank Tripathi Date: Wed, 21 Oct 2020 21:22:29 +0530 Subject: [PATCH] Added compatibitily for django 3.x.x --- simple_autocomplete/monkey.py | 2 +- simple_autocomplete/utils.py | 14 +++++++------- simple_autocomplete/widgets.py | 31 ++++++++++++++----------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/simple_autocomplete/monkey.py b/simple_autocomplete/monkey.py index 62d25ba..0c57e6d 100644 --- a/simple_autocomplete/monkey.py +++ b/simple_autocomplete/monkey.py @@ -100,7 +100,7 @@ def ModelChoiceField__init__(self, queryset, empty_label="---------", # Call Field instead of ChoiceField __init__() because we don't need # ChoiceField.__init__(). - Field.__init__(self, required, widget, label, initial, help_text, + Field.__init__(self, required=required, widget=widget, label=label, initial=initial, help_text=help_text, *args, **kwargs) self.queryset = queryset self.limit_choices_to = limit_choices_to # limit the queryset later. diff --git a/simple_autocomplete/utils.py b/simple_autocomplete/utils.py index ecf22a2..4296811 100644 --- a/simple_autocomplete/utils.py +++ b/simple_autocomplete/utils.py @@ -1,4 +1,4 @@ -from django.db.models.fields import FieldDoesNotExist, CharField +from django.db.models.fields import CharField from django.conf import settings @@ -6,8 +6,8 @@ def get_search_fieldname(model): # If model has 'search_field' settings use that. Otherwise, if # model has field 'title' then use that, else use the first # CharField on model. - fieldname = get_setting("%s.%s" % (model._meta.app_label, model.__name__.lower()), \ - 'search_field', '') + fieldname = get_setting("%s.%s" % (model._meta.app_label, model.__name__.lower()), + 'search_field', '') if fieldname: try: # Django 1.11 deprecates get_field_by_name @@ -15,9 +15,9 @@ def get_search_fieldname(model): model._meta.get_field_by_name(fieldname) else: model._meta.get_field(fieldname) - except FieldDoesNotExist: - raise RuntimeError("Field '%s.%s' does not exist" % (model._meta.app_label, \ - model.__name__.lower())) + except: + raise RuntimeError("Field '%s.%s' does not exist" % (model._meta.app_label, + model.__name__.lower())) else: try: # Django 1.11 deprecates get_field_by_name @@ -26,7 +26,7 @@ def get_search_fieldname(model): else: model._meta.get_field('title') fieldname = 'title' - except FieldDoesNotExist: + except: for field in model._meta.fields: if isinstance(field, CharField): fieldname = field.name diff --git a/simple_autocomplete/widgets.py b/simple_autocomplete/widgets.py index dd0ee41..34303d3 100644 --- a/simple_autocomplete/widgets.py +++ b/simple_autocomplete/widgets.py @@ -3,15 +3,10 @@ from django.forms.widgets import Select, SelectMultiple from django.utils.safestring import mark_safe from django.db.models.query import QuerySet -from django.conf import settings -try: - from django.urls import reverse -except: - from django.core.urlresolvers import reverse +from django.urls import reverse from simple_autocomplete.monkey import _simple_autocomplete_queryset_cache -from simple_autocomplete.utils import get_search_fieldname, \ - get_threshold_for_model +from simple_autocomplete.utils import get_threshold_for_model class AutoCompleteWidget(Select): @@ -21,8 +16,7 @@ class AutoCompleteWidget(Select): token = None model = None - def __init__(self, url=None, initial_display=None, token=None, - model=None, *args, **kwargs): + def __init__(self, url=None, initial_display=None, token=None, model=None, *args, **kwargs): """ url: a custom URL that returns JSON with format [(value, label),(value, label),...]. @@ -41,7 +35,7 @@ def __init__(self, url=None, initial_display=None, token=None, self.model = model super(AutoCompleteWidget, self).__init__(*args, **kwargs) - def render(self, name, value, attrs=None): + def render(self, name, value, attrs=None, renderer=None): if value is None: value = '' @@ -106,8 +100,10 @@ def render(self, name, value, attrs=None): -x -""" % dict(name=name, url=url, display=display, value=value) +x +""" % dict(name=name, url=url, + display=display, value=value) return mark_safe(html) @@ -118,8 +114,7 @@ class AutoCompleteMultipleWidget(SelectMultiple): token = None model = None - def __init__(self, url=None, initial_display=None, token=None, - model=None, *args, **kwargs): + def __init__(self, url=None, initial_display=None, token=None, model=None, *args, **kwargs): """ url: a custom URL that returns JSON with format [(value, label),(value, label),...]. @@ -140,7 +135,7 @@ def __init__(self, url=None, initial_display=None, token=None, self.model = model super(AutoCompleteMultipleWidget, self).__init__(*args, **kwargs) - def render(self, name, value, attrs=None): + def render(self, name, value, attrs=None, renderer=None): if value is None: value = [] @@ -196,7 +191,8 @@ def render(self, name, value, attrs=None): var target = $('div.autocomplete-placeholder', parent); target.append('

' + ui.item.value - + ' x

'); + + ' x

'); }, close: function(event, ui) { django.jQuery('#id_%s_helper').val(''); @@ -223,7 +219,8 @@ def render(self, name, value, attrs=None): display = str(obj) html += """

-%s x

""" % (name, v, display, name, name) +%s x

""" % (name, v, display, name, name) html += ""