From 858ab53725fe8f89060c86132db3f6da5e44a82f Mon Sep 17 00:00:00 2001 From: N1K1TAS95 <46042802+N1K1TAS95@users.noreply.github.com> Date: Fri, 28 Oct 2022 20:36:42 +0200 Subject: [PATCH 1/3] Added SELECT2_JSON_ENCODER Added support to set a custom JSON encoder via Django settings.py --- django_select2/conf.py | 26 ++++++++++++++++++++++++++ django_select2/views.py | 4 +++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/django_select2/conf.py b/django_select2/conf.py index 8862932a..4f5a03d0 100644 --- a/django_select2/conf.py +++ b/django_select2/conf.py @@ -198,6 +198,32 @@ class Select2Conf(AppConf): ``settings.DJANGO_SELECT2_I18N`` refers to :attr:`.I18N_PATH`. """ + """ + Custo JSON encoder class used for encoding JSON response used in ModelSelect2Widget, ModelSelect2MultipleWidget + and ModelSelect2TagWidget. + Useful when your models uses some special proxy fields and + you get error: Object of type is not JSON serializable + + Default value is: django.core.serializers.json.DjangoJSONEncoder + + For example: + + If you use django-hashid-field package, you'll get an Object of type Hashid is not JSON serializable when you try + to use ModelSelect2Widget. + Just extend DjangoJSONEncoder: + from django.core.serializers.json import DjangoJSONEncoder + from hashid_field import Hashid + + class HashidJSONEncoder(DjangoJSONEncoder): + def default(self, o): + if isinstance(o, Hashid): + return str(o) + return super().default(o) + + And set SELECT2_JSON_ENCODER to 'app_name.econders.HashidJSONEncoder' in settings.py of your project. + """ + JSON_ENCODER = 'django.core.serializers.json.DjangoJSONEncoder' + 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): From f5b2fb4bf2784bbc98acb715103dff1860bb08de Mon Sep 17 00:00:00 2001 From: N1K1TAS95 <46042802+N1K1TAS95@users.noreply.github.com> Date: Sun, 30 Oct 2022 18:17:36 +0100 Subject: [PATCH 2/3] Update django_select2/conf.py Co-authored-by: Johannes Maron --- django_select2/conf.py | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/django_select2/conf.py b/django_select2/conf.py index 4f5a03d0..c5e9c7cc 100644 --- a/django_select2/conf.py +++ b/django_select2/conf.py @@ -199,28 +199,10 @@ class Select2Conf(AppConf): """ """ - Custo JSON encoder class used for encoding JSON response used in ModelSelect2Widget, ModelSelect2MultipleWidget - and ModelSelect2TagWidget. - Useful when your models uses some special proxy fields and - you get error: Object of type is not JSON serializable - - Default value is: django.core.serializers.json.DjangoJSONEncoder - - For example: - - If you use django-hashid-field package, you'll get an Object of type Hashid is not JSON serializable when you try - to use ModelSelect2Widget. - Just extend DjangoJSONEncoder: - from django.core.serializers.json import DjangoJSONEncoder - from hashid_field import Hashid - - class HashidJSONEncoder(DjangoJSONEncoder): - def default(self, o): - if isinstance(o, Hashid): - return str(o) - return super().default(o) - - And set SELECT2_JSON_ENCODER to 'app_name.econders.HashidJSONEncoder' in settings.py of your project. + 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. """ JSON_ENCODER = 'django.core.serializers.json.DjangoJSONEncoder' From b53cd8eb9c7bf0d2e02ec909a14e842b994dc5f9 Mon Sep 17 00:00:00 2001 From: N1K1TAS95 <46042802+N1K1TAS95@users.noreply.github.com> Date: Sun, 30 Oct 2022 18:26:39 +0100 Subject: [PATCH 3/3] Moved documentation under the attribute --- django_select2/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_select2/conf.py b/django_select2/conf.py index c5e9c7cc..a3117014 100644 --- a/django_select2/conf.py +++ b/django_select2/conf.py @@ -198,13 +198,13 @@ 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. """ - JSON_ENCODER = 'django.core.serializers.json.DjangoJSONEncoder' class Meta: """Prefix for all Django-Select2 settings."""