Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compatibitily for django 3.x.x #27

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion simple_autocomplete/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions simple_autocomplete/utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from django.db.models.fields import FieldDoesNotExist, CharField
from django.db.models.fields import CharField
from django.conf import settings


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
if hasattr(model._meta, 'get_field_by_name'):
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
Expand All @@ -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
Expand Down
31 changes: 14 additions & 17 deletions simple_autocomplete/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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),...].
Expand All @@ -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 = ''

Expand Down Expand Up @@ -106,8 +100,10 @@ def render(self, name, value, attrs=None):
</script>

<input id="id_%(name)s_helper" type="text" value="%(display)s" />
<a href="#" title="Clear" onclick="django.jQuery('#id_%(name)s_helper').val(''); django.jQuery('#id_%(name)s_helper').focus(); django.jQuery('#id_%(name)s').val(''); return false;">x<small></small></a>
<input name="%(name)s" id="id_%(name)s" type="hidden" value="%(value)s" />""" % dict(name=name, url=url, display=display, value=value)
<a href="#" title="Clear" onclick="django.jQuery('#id_%(name)s_helper').val('');
django.jQuery('#id_%(name)s_helper').focus(); django.jQuery('#id_%(name)s').val(''); return false;">x<small></small></a>
<input name="%(name)s" id="id_%(name)s" type="hidden" value="%(value)s" />""" % dict(name=name, url=url,
display=display, value=value)
return mark_safe(html)


Expand All @@ -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),...].
Expand All @@ -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 = []

Expand Down Expand Up @@ -196,7 +191,8 @@ def render(self, name, value, attrs=None):
var target = $('div.autocomplete-placeholder', parent);
target.append('<p><input name="' + name + '" value="' + ui.item.real_value + '" '
+ 'type="hidden" />' + ui.item.value
+ ' <a href="#" title="Remove" onclick="django.jQuery(this).parent().remove(); django.jQuery('+"'"+'#id_%s_helper'+"'"+').val(' + "''" + '); django.jQuery('+"'"+'#id_%s_helper'+"'"+').focus(); return false;">x<small></small></a></p>');
+ ' <a href="#" title="Remove" onclick="django.jQuery(this).parent().remove();
django.jQuery('+"'"+'#id_%s_helper'+"'"+').val(' + "''" + '); django.jQuery('+"'"+'#id_%s_helper'+"'"+').focus(); return false;">x<small></small></a></p>');
},
close: function(event, ui) {
django.jQuery('#id_%s_helper').val('');
Expand All @@ -223,7 +219,8 @@ def render(self, name, value, attrs=None):
display = str(obj)

html += """<p><input name="%s" type="hidden" value="%s" />
%s <a href="#" title="Remove" onclick="django.jQuery(this).parent().remove(); django.jQuery('#id_%s_helper').val(''); django.jQuery('#id_%s_helper').focus(); return false;">x<small></small></a></p>""" % (name, v, display, name, name)
%s <a href="#" title="Remove" onclick="django.jQuery(this).parent().remove(); django.jQuery('#id_%s_helper').val('');
django.jQuery('#id_%s_helper').focus(); return false;">x<small></small></a></p>""" % (name, v, display, name, name)

html += "</div>"

Expand Down