Skip to content

Commit

Permalink
Resolve #159 -- Add custom JSONEncoder support via setting (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
N1K1TAS95 authored Oct 30, 2022
1 parent 7215346 commit 5a635fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions django_select2/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<json.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."""

Expand Down
4 changes: 3 additions & 1 deletion django_select2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 5a635fe

Please sign in to comment.