55from django import forms
66from django .conf import settings
77from django .contrib .contenttypes .models import ContentType
8- from django .core .serializers .json import DjangoJSONEncoder
98from django .db .models import Model
109from django .forms .utils import flatatt
1110from django .template .defaultfilters import force_escape
1413from django .utils .safestring import mark_safe
1514from django .utils .six import text_type
1615from django .utils .translation import ugettext as _
16+ from django .utils .module_loading import import_string
1717
1818from ajax_select .registry import registry
1919
@@ -40,6 +40,10 @@ def _media(self):
4040 return forms .Media (css = {'all' : ('ajax_select/css/ajax_select.css' ,)}, js = js )
4141
4242
43+ json_encoder = import_string (getattr (settings , 'AJAX_SELECT_JSON_ENCODER' ,
44+ 'django.core.serializers.json.DjangoJSONEncoder' ))
45+
46+
4347###############################################################################
4448
4549
@@ -227,7 +231,7 @@ def render(self, name, value, attrs=None):
227231 'current' : value ,
228232 'current_ids' : current_ids ,
229233 'current_reprs' : mark_safe (
230- json .dumps (initial , cls = DjangoJSONEncoder )
234+ json .dumps (initial , cls = json_encoder )
231235 ),
232236 'help_text' : help_text ,
233237 'extra_attrs' : mark_safe (flatatt (final_attrs )),
@@ -439,7 +443,10 @@ def autoselect_fields_check_can_add(form, model, user):
439443 for name , form_field in form .declared_fields .items ():
440444 if isinstance (form_field , (AutoCompleteSelectMultipleField , AutoCompleteSelectField )):
441445 db_field = model ._meta .get_field (name )
442- form_field .check_can_add (user , db_field .rel .to )
446+ if hasattr (db_field , "remote_field" ):
447+ form_field .check_can_add (user , db_field .remote_field .model )
448+ else :
449+ form_field .check_can_add (user , db_field .rel .to )
443450
444451
445452def make_plugin_options (lookup , channel_name , widget_plugin_options , initial ):
@@ -457,9 +464,9 @@ def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
457464 po ['html' ] = True
458465
459466 return {
460- 'plugin_options' : mark_safe (json .dumps (po , cls = DjangoJSONEncoder )),
467+ 'plugin_options' : mark_safe (json .dumps (po , cls = json_encoder )),
461468 'data_plugin_options' : force_escape (
462- json .dumps (po , cls = DjangoJSONEncoder )
469+ json .dumps (po , cls = json_encoder )
463470 )
464471 }
465472
0 commit comments