diff --git a/django_select2/conf.py b/django_select2/conf.py index 8862932a..a3117014 100644 --- a/django_select2/conf.py +++ b/django_select2/conf.py @@ -198,6 +198,14 @@ class Select2Conf(AppConf): ``settings.DJANGO_SELECT2_I18N`` refers to :attr:`.I18N_PATH`. """ + JSON_ENCODER = 'django.core.serializers.json.DjangoJSONEncoder' + """ + A :class:`JSONEncoder` used to generate the API response for the model widgets. + + A custom JSON encoder might be useful when your models uses + a special primary key, that isn't serializable by the default encoder. + """ + class Meta: """Prefix for all Django-Select2 settings.""" diff --git a/django_select2/views.py b/django_select2/views.py index c03f683e..0af69b64 100644 --- a/django_select2/views.py +++ b/django_select2/views.py @@ -3,6 +3,7 @@ from django.core.signing import BadSignature from django.http import Http404, JsonResponse from django.views.generic.list import BaseListView +from django.utils.module_loading import import_string from .cache import cache from .conf import settings @@ -43,7 +44,8 @@ def get(self, request, *args, **kwargs): for obj in context["object_list"] ], "more": context["page_obj"].has_next(), - } + }, + encoder=import_string(settings.SELECT2_JSON_ENCODER) ) def get_queryset(self):