Skip to content

Commit 5a635fe

Browse files
authored
Resolve #159 -- Add custom JSONEncoder support via setting (#160)
1 parent 7215346 commit 5a635fe

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

django_select2/conf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ class Select2Conf(AppConf):
198198
``settings.DJANGO_SELECT2_I18N`` refers to :attr:`.I18N_PATH`.
199199
"""
200200

201+
JSON_ENCODER = 'django.core.serializers.json.DjangoJSONEncoder'
202+
"""
203+
A :class:`JSONEncoder<json.JSONEncoder>` used to generate the API response for the model widgets.
204+
205+
A custom JSON encoder might be useful when your models uses
206+
a special primary key, that isn't serializable by the default encoder.
207+
"""
208+
201209
class Meta:
202210
"""Prefix for all Django-Select2 settings."""
203211

django_select2/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.core.signing import BadSignature
44
from django.http import Http404, JsonResponse
55
from django.views.generic.list import BaseListView
6+
from django.utils.module_loading import import_string
67

78
from .cache import cache
89
from .conf import settings
@@ -43,7 +44,8 @@ def get(self, request, *args, **kwargs):
4344
for obj in context["object_list"]
4445
],
4546
"more": context["page_obj"].has_next(),
46-
}
47+
},
48+
encoder=import_string(settings.SELECT2_JSON_ENCODER)
4749
)
4850

4951
def get_queryset(self):

0 commit comments

Comments
 (0)