11from __future__ import unicode_literals
2+
23import json
3- from ajax_select . registry import registry
4+
45from django import forms
56from django .conf import settings
67from django .contrib .contenttypes .models import ContentType
7- from django .db .models .query import QuerySet
8+ from django .core .serializers .json import DjangoJSONEncoder
9+ from django .db .models import Model
810from django .forms .utils import flatatt
911from django .template .defaultfilters import force_escape
1012from django .template .loader import render_to_string
1113from django .utils .encoding import force_text
1214from django .utils .safestring import mark_safe
1315from django .utils .six import text_type
1416from django .utils .translation import ugettext as _
17+
18+ from ajax_select .registry import registry
19+
1520try :
1621 from django .urls import reverse
1722except ImportError :
@@ -96,7 +101,8 @@ def render(self, name, value, attrs=None):
96101 'func_slug' : self .html_id .replace ("-" , "" ),
97102 'add_link' : self .add_link ,
98103 }
99- context .update (make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
104+ context .update (
105+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
100106 templates = (
101107 'ajax_select/autocompleteselect_%s.html' % self .channel ,
102108 'ajax_select/autocompleteselect.html' )
@@ -127,7 +133,8 @@ def __init__(self, channel, *args, **kwargs):
127133 )
128134 widget_kwargs .update (kwargs .pop ('widget_options' , {}))
129135 kwargs ["widget" ] = AutoCompleteSelectWidget (** widget_kwargs )
130- super (AutoCompleteSelectField , self ).__init__ (max_length = 255 , * args , ** kwargs )
136+ super (AutoCompleteSelectField , self ).__init__ (
137+ max_length = 255 , * args , ** kwargs )
131138
132139 def clean (self , value ):
133140 if value :
@@ -138,7 +145,8 @@ def clean(self, value):
138145 # or your channel is faulty
139146 # out of the scope of this field to do anything more than
140147 # tell you it doesn't exist
141- raise forms .ValidationError ("%s cannot find object: %s" % (lookup , value ))
148+ raise forms .ValidationError (
149+ "%s cannot find object: %s" % (lookup , value ))
142150 return objs [0 ]
143151 else :
144152 if self .required :
@@ -194,10 +202,11 @@ def render(self, name, value, attrs=None):
194202
195203 lookup = registry .get (self .channel )
196204
197- if isinstance (value , QuerySet ):
198- objects = value
205+ values = list (value )
206+ if all ([isinstance (v , Model ) for v in values ]):
207+ objects = values
199208 else :
200- objects = lookup .get_objects (value )
209+ objects = lookup .get_objects (values )
201210
202211 current_ids = pack_ids ([obj .pk for obj in objects ])
203212
@@ -217,15 +226,18 @@ def render(self, name, value, attrs=None):
217226 'html_id' : self .html_id ,
218227 'current' : value ,
219228 'current_ids' : current_ids ,
220- 'current_reprs' : mark_safe (json .dumps (initial )),
229+ 'current_reprs' : mark_safe (
230+ json .dumps (initial , cls = DjangoJSONEncoder )
231+ ),
221232 'help_text' : help_text ,
222233 'extra_attrs' : mark_safe (flatatt (final_attrs )),
223234 'func_slug' : self .html_id .replace ("-" , "" ),
224235 'add_link' : self .add_link ,
225236 }
226- context .update (make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
237+ context .update (
238+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
227239 templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self .channel ,
228- 'ajax_select/autocompleteselectmultiple.html' )
240+ 'ajax_select/autocompleteselectmultiple.html' )
229241 out = render_to_string (templates , context )
230242 return mark_safe (out )
231243
@@ -269,7 +281,8 @@ def __init__(self, channel, *args, **kwargs):
269281 dh = 'Hold down "Control", or "Command" on a Mac, to select more than one.'
270282 django_default_help = _ (dh ).translate (settings .LANGUAGE_CODE )
271283 if django_default_help in translated :
272- cleaned_help = translated .replace (django_default_help , '' ).strip ()
284+ cleaned_help = translated .replace (
285+ django_default_help , '' ).strip ()
273286 # probably will not show up in translations
274287 if cleaned_help :
275288 help_text = cleaned_help
@@ -358,13 +371,15 @@ def render(self, name, value, attrs=None):
358371 'extra_attrs' : mark_safe (flatatt (final_attrs )),
359372 'func_slug' : self .html_id .replace ("-" , "" ),
360373 }
361- context .update (make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
374+ context .update (
375+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
362376 templates = ('ajax_select/autocomplete_%s.html' % self .channel ,
363377 'ajax_select/autocomplete.html' )
364378 return mark_safe (render_to_string (templates , context ))
365379
366380
367381class AutoCompleteField (forms .CharField ):
382+
368383 """
369384 A CharField that uses an AutoCompleteWidget to lookup matching
370385 and stores the result as plain text.
@@ -411,7 +426,8 @@ def _check_can_add(self, user, related_model):
411426 if can_add :
412427 app_label = related_model ._meta .app_label
413428 model = related_model ._meta .object_name .lower ()
414- self .widget .add_link = reverse ('admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
429+ self .widget .add_link = reverse (
430+ 'admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
415431
416432
417433def autoselect_fields_check_can_add (form , model , user ):
@@ -441,8 +457,10 @@ def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
441457 po ['html' ] = True
442458
443459 return {
444- 'plugin_options' : mark_safe (json .dumps (po )),
445- 'data_plugin_options' : force_escape (json .dumps (po ))
460+ 'plugin_options' : mark_safe (json .dumps (po , cls = DjangoJSONEncoder )),
461+ 'data_plugin_options' : force_escape (
462+ json .dumps (po , cls = DjangoJSONEncoder )
463+ )
446464 }
447465
448466
0 commit comments